15.5 Rendering Cells


You 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 Interface

This 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:

public abstract Component getTableCellRendererComponent(JTable table, Object
value, boolean isSelected, boolean hasFocus, int row, int column)

This model takes a value, which can also be retrieved by getting the cell at row, column of table, and returns a component capable of drawing the value in a table cell (or anywhere, really). The resulting drawing can be affected by the selection state of the object and whether it currently has the keyboard focus.

15.5.2 The DefaultTableCellRenderer Class

The 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 Properties

The 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.

Table 15-15. DefaultTableCellRenderer properties

Property

Data type

get

is

set

Default value

background

Color

·

 

·

null

foreground

Color

·

 

·

null

opaque

boolean

 

·

·

true

See also properties from the JLabel class (Table 4-1).

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
figs/swng2.1509.gif

15.5.3 The CellRendererPane Class

This 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.



Java Swing
Graphic Java 2: Mastering the Jfc, By Geary, 3Rd Edition, Volume 2: Swing
ISBN: 0130796670
EAN: 2147483647
Year: 2001
Pages: 289
Authors: David Geary

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net