The table created when using an instance of the TestTableModel as the data model for an instance of the JTable class is shown in Figure 12.6. The model can be set for the table by calling the setModel method or by specifying the model in the JTable classs constructor. This is shown in the following line of code.
JTable table = new JTable( new TableTestModel() );
This model can be added to the SimpleTableTest application, and it is the model used for the TableTest application developed in this section.
You will immediately notice the change in this table shown in Figure 12.6 to those shown earlier in this chapter. When the Boolean class is returned from the getColumnClass method in the TestTableModel method, the JTable class uses a JCheckBox for the cell renderer, and when a Number is returned, the text is right aligned. This magic is enabled in the protected createDefaultRenderers method in the JTable class. This method creates and registers renders for known classes by using the setDefaultRenderer method. The set of renderers registered is shown in Table 12.5.
Table 12.5 Default Renderers for the JTable Class
|
Class
| Renderer
| Positioning
|
|
Object
| DefaultTableCellRenderer
| Left-aligned text
|
Number
| DefaultTableCellRenderer
| Right-aligned text
|
ImageIcon
| DefaultTableCellRenderer
| Centered Icon
|
Boolean
| CheckBoxRenderer
| Centered JCheckBox
|
|
Extensions of the JTable class can override the createDefaultRenderers method to establish a different set of renderers. Also, the setDefaultRenderer method is public. This allows clients of the JTable class to change the default renderer or add renderers for additional classes of objects. When a renderer is registered for a class, it is used to render all columns in the model that contain that class of data. Thus, registering a renderer for a class is a global setting, while setting the renderer for a TableColumn instance is only valid for that column, even if multiple columns contain that class of data.
If set, the ToolTip for the table is retrieved from the renderer. If the renderer does not have a ToolTip, the tip is retrieved from the table. This allows custom renderers to contain ToolTips specific for the class of data that they render. ToolTips are enabled for JTable instances by default. However, calling the setToolTipText method with a null parameter will cause the table to remove itself from the ToolTipManager.
The default rendering of the JTable class presents the Boolean instances in the table appropriately. However, as you can see in Figure 12.6, the cell containing the JCheckBox isnt rendered the same as other cells in its row. Also, the Color data still leaves room for improvement. Registering your own renderer for the Color class makes this improvement. A new renderer for the Boolean class that paints the same color as the other cells is also registered. This has the effect of replacing the default Boolean renderer. Listing 12.7 contains the code for the new renderers and registers them with the table. The Boolean renderer is a complete class that extends the JCheckBox class and implements the TableCellRenderer interface. The majority of the classs work is performed in the getTableCellRendererComponent method. In this method, the check box is configured for the current state of the cell that will be rendered. The selected state for the check box is set based on the value passed to the method. If the cell is selected, the selected foreground and background colors are queried from the table and set on the check box. Similar processing occurs for an unselected cell. Finally, the check box is returned as the rendering component.
The color renderer is made by creating an anonymous class that extends the DefaultTableCellRenderer class and sets its background color in the setValue method. The renderer must be made opaque so that it paints its background. This makes the color renderer very simple. However, if you wish to use this technique in multiple tables, it should be made into a standalone class.
|