I l@ve RuBoard |
![]() ![]() |
15.5 Rendering CellsYou can build your own renderers for the cells in your table. By default, you get renderers for Boolean types (JCheckBox for display and editing), ImageIcon types, Number types (right-justified JTextField), and Object types (JTextField). However, you can specify a particular renderer for a class type or for a particular column, or even for a particular cell. 15.5.1 The TableCellRenderer InterfaceThis interface provides access to a rendering component without defining what the component does. This works because a renderer functions by rubber-stamping a component's image in a given location. The only method this interface defines initializes and returns just such a component:
15.5.2 The DefaultTableCellRenderer ClassThe javax.swing.table package includes a default renderer that produces a JLabel to display text for each cell in the table. The JTable class uses this renderer to display Numbers, Icons, and Objects. JTable creates a new default renderer and then aligns it correctly and attaches an appropriate icon, depending on the type of data. Object objects are converted to strings using toString( ) and are shown as regular labels. Number objects are shown right-aligned, and Icons are shown using centered labels. Boolean values do not use DefaultTableCellRenderer; instead, they use a private renderer class that extends JCheckBox. Go back and take a look at Figure 15-5 for an example of how this renderer works on different types of data. 15.5.2.1 PropertiesThe DefaultTableCellRenderer modifies three properties of the JLabel class, as shown in Table 15-15. The color values are used as the "unselected" foreground and background colors for text. You might recall that the selected foreground and background colors are governed by the JTable class. If you set either of these properties to null, the foreground and background colors from JTable are used.
Of course, we can also build our own renderer based on DefaultTableCellRenderer. Here's an example renderer we can use with our FileModel from Section 15.3.2.1. This renderer puts an exclamation point icon in front of any file size greater than some threshold value (passed to the constructor of our renderer). // BigRenderer.java // A renderer for numbers that shows an icon in front of big numbers // import java.awt.*; import javax.swing.*; import javax.swing.table.*; public class BigRenderer extends DefaultTableCellRenderer { double threshold; Icon bang = new ImageIcon("bang.gif"); public BigRenderer(double t) { threshold = t; setHorizontalAlignment(JLabel.RIGHT); setHorizontalTextPosition(SwingConstants.RIGHT); } public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) { // Be a little paranoid about where the user tries to use this renderer. if (value instanceof Number) { if (((Number)value).doubleValue( ) > threshold) { setIcon(bang); } else { setIcon(null); } } else { setIcon(null); } return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col); } } To attach this renderer to our table, we add a few lines of code to the FileTable class: JTable jt = new JTable(fm); // ... jt.setDefaultRenderer(Number.class, new BigRenderer(1000)); Figure 15-9 shows the results of this renderer in action with the new FileTable2 class. Figure 15-9. A custom renderer (note the icons) applied to a file information table![]() 15.5.3 The CellRendererPane ClassThis utility class was built to keep renderers from propagating repaint( ) and validate( ) calls to the components using renderer components such as JTree and JList. If you played around with creating your own renderers for any of the Swing components that use them, you'll recall that you did not use this class yourself. This pane is often wrapped around the renderer, and its various paintComponent( ) methods are used to do the actual drawing. You do not normally need to worry about this class. ![]() |
I l@ve RuBoard |
![]() ![]() |