- Colored ToolTips
- Detachable/Non-Detachable Toolbars
- Individualized List ToolTips
- Non-Auto-Resize Table Columns
- Table Header Icons
Non-Auto–Resize Table Columns
When a table component is created from the JTable class, JTable automatically resizes columns horizontally so they all appear in the table's window. Figure 6 illustrates this scenario.
By default, a JTable component automatically resizes its columns so they all appear in the window.
Automatic resizing can be disabled by calling JTable's setAutoResizeMode method with an AUTO_RESIZE_OFF argument. As a result, a column might be partially displayed and other columns might be hidden. However, the additional room makes it possible to see more column information (see Figure 7).
A JTable component's automatic column resizing can be disabled.
The GUI in Figure 7 was generated by an application called NARTableColumns. The source code to this application (shown in Listing 4) demonstrates calling setAutoResizeMode with an AUTO_RESIZE_OFF argument. (Check out the SDK 1.3 documentation on this method to learn what other arguments can be passed.)
Listing 4 The NARTableColumns application source code
// NARTableColumns.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; class NARTableColumns extends JFrame { NARTableColumns (String title) { // Pass the title argument to the JFrame superclass so that the // contents of title appear in the JFrame's title bar. super (title); // Exit the application by calling System.exit when the user // either selects Close from the System menu or clicks the X // button. setDefaultCloseOperation (EXIT_ON_CLOSE); // Define the data model for the JTable. TableModel dataModel = new AbstractTableModel () { final static int NROWS = 20; final static int NCOLS = 5; Object [][] rowData = new Object [NROWS] []; // This instance initializer creates the data when an object // is created from this subclass of the AbstractTableModel // class. { for (int i = 0; i < NROWS; i++) rowData [i] = new Object [NCOLS]; int x = 0; for (int row = 0; row < NROWS; row++) for (int col = 0; col < NCOLS; col++) rowData [row] [col] = new Integer (2 * x++); } public int getColumnCount () { return NCOLS; } public int getRowCount () { return NROWS; } public Object getValueAt (int row, int col) { return rowData [row] [col]; } public boolean isCellEditable (int row, int col) { return false; } public void setValueAt (Object value, int row, int col) { rowData [row] [col] = value; fireTableCellUpdated (row, col); } }; // Create the JTable and assign it the predefined data model. JTable table = new JTable (dataModel); // Do not allow table columns to be automatically resized so // they all fit within the Frame window. table.setAutoResizeMode (JTable.AUTO_RESIZE_OFF); // Create a JScrollPane and add the JTable to this container. JScrollPane sp = new JScrollPane (table); // Set the JTable's preferred "viewport" size for viewing // JTable data. table.setPreferredScrollableViewportSize (new Dimension (250, 180)); // Add the JScrollPane to the JFrame's content pane. getContentPane ().add (sp); // Set the size of the JFrame window. setSize (300, 250); // Show the JFrame window. setVisible (true); } public static void main (String [] args) { new NARTableColumns ("Non-auto-resize Table Columns"); } }