![]() |
|||
![]() ![]() |
![]() |
|
![]() |
The SimpleTableTest application creates a bare-bones table. It doesnt display the column names, and the column width is too small for the second column. To create a more pleasing table, more than just the constructor needs to be called. The table header can be queried from the table by calling the getTableHeader method and arranged anywhere you want. Typically, the header is shown above the data. The following code creates the table and displays its header above the table. The resulting table is shown in Figure 12.3. JTable table = new JTable( data, columnNames ); JTableHeader tableHeader = table.getTableHeader(); JPanel tablePanel = new JPanel(); tablePanel.setBorder( BorderFactory.createLoweredBevelBorder() ); tablePanel.setLayout( new BorderLayout() ); tablePanel.add( tableHeader, BorderLayout.NORTH ); tablePanel.add( table, BorderLayout.CENTER ); frame.getContentPane().add( tablePanel, BorderLayout.CENTER );
The header is an instance of the JTableHeader class contained in the java.awt.swing.table package. It allows the area between two columns to be dragged to resize the columns. The column name itself can be dragged to change the order of the columns in the table. (This is the default behavior of the JTableHeader class; later you will see how to change this.) If this code seems like a lot of work to display the columns for a table, you are correct; it is. The JTable class contains specialized code for handling the typical configuration of a table, that being the table placed in a scroll pane. (The JScrollPane class is discussed in Chapter 15, Scrolling Components.) The JTable class detects when it is added to a JScrollPane and configures itself appropriately. The configuration is performed in the protected configureEnclosingScrollPane method. This protected method allows extensions to the JTable class to alter a tables configuration when it is added to a JScrollPane instance. The earlier example can be simplified to the following code that creates the table shown in Figure 12.4. JTable table = new JTable( data, columnNames ); frame.getContentPane().add( new JScrollPane( table ), BorderLayout.CENTER );
After the table has been placed in a JScrollPane, its scrolling behavior is specified in the JTable class. The JTable class implements the Scrollable interface. This interface is discussed in Chapter 15. The JTable class returns false from the getScrollableTracksViewportWidth and getScrollableTracksViewportHeight methods to signify that the table width and height are not determined by the scroll panes viewport size. The JTable class returns the height of a row for the vertical unit scrolling increment and one pixel for the horizontal unit scroll increment. It would be an improvement if the horizontal scrolling increment could be set to the width of the column being scrolled out of view. The block scrolling size is set to both the horizontal and vertical viewport size. This enables one page of the table to be scrolled in block mode. The preferred size for the viewport can be set by using the setPreferredScrollableViewportSize method. This value is used by the viewport to set its size. Passing false to the setShowGrid method will hide the grid displayed in the table. Also, you can show only the vertical or horizontal portion of the grid with the setShow-HorizontalLines and setShowVerticalLine methods. Passing true or false to these methods will show or hide the lines in the table. If the table in the SimpleTableTest application is created as follows, only the horizontal lines in the table will be displayed. This is seen in Figure 12.5. The color of the grid can be set with the setGridColor method. The getGridColor method returns the current value of the grid. Neither the display state or color of the grid are bound properties in the JTable class. JTable table = new JTable( data, columnNames ); table.setShowGrid( false ); table.setShowHorizontalLines( true ); table.setGridColor( Color.blue ); frame.getContentPane().add( new JScrollPane( table ), BorderLayout.CENTER );
|
![]() |
|