Figure 12.7 also shows the cell containing the color red being edited. You can see that the editor is a JTextField with the text set to the String returned from the toString method of the Color instance. This is obviously undesirable behavior. The solution is to create a cell editor and set it for the Color class.
Similar to what you saw previously for renderers, the JTable class calls the protected createDefaultEditors method to create and register default editors for the table. Table 12.6 lists the classes for which default editors are registered. Extensions to the JTable class can override this method to register a different set of editors for the table. Clients can also register editors by using the public setDefaultEditor method.
Table 12.6 Default Editors for the JTable Class
|
Class
| Renderer
| Positioning
|
|
Object
| DefaultCellEditor
| JTextField with left-aligned text
|
Number
| DefaultCellEditor
| JTextField with right-aligned text
|
Boolean
| DefaultCellEditor
| Centered JCheckBox
|
|
The code shown next shows an editor that allows the colors to be chosen in the table. The editor is a DefaultCellEditor containing a JComboBox that has been configured for the valid colors for the data model. If any color is allowed in the data model, an editor could be created from the JColorChooser class. This class is presented in Chapter 19, Choice Dialog Boxes. The JComboBox is also configured with the renderer shown in Listing 12.8. This renderer displays colors similarly to the renderer in the table. The resulting editor is shown in Figure 12.8.
JComboBox comboBox = new JComboBox();
comboBox.addItem( Color.black );
comboBox.addItem( Color.gray );
comboBox.addItem( Color.red );
comboBox.addItem( Color.pink );
comboBox.addItem( Color.orange );
comboBox.addItem( Color.yellow );
comboBox.addItem( Color.green );
comboBox.addItem( Color.blue );
comboBox.addItem( Color.cyan );
comboBox.addItem( Color.white );
comboBox.addItem( Color.lightGray );
comboBox.setRenderer( new ColorCellRenderer() );
table.setDefaultEditor( Color.class, new DefaultCellEditor( comboBox ) );
Listing 12.8 The ColorCellRenderer Class
/**
* ColorCellRenderer is a rendering class that expects
* Color values. When this is the case, it paints the color
* as the background of the label. If not, show the String
* returned from the toString method of the value Object.
*
* @author Mike Foley
**/
class ColorCellRenderer extends JLabel implements ListCellRenderer {
Border selectedWhiteBorder;
Border selectedBlackBorder;
/**
* ColorCellRenderer, default constructor.
* We must be opaque so the background is painted.
**/
public ColorCellRenderer() {
setOpaque( true );
selectedWhiteBorder = BorderFactory.createMatteBorder(
3, 5, 3, 5, Color.white );
selectedBlackBorder = BorderFactory.createMatteBorder(
3, 5, 3, 5, Color.black );
}
/**
* Configure yourself for the state passed.
* If the value is a Color, set the background to that
* color. If not use the toString method of the value
*
**/
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
{
if( value instanceof Color ) {
setText( );
setBackground( ( Color )value );
if( isSelected ) {
if( value.equals( Color.white ) )
setBorder( selectedBlackBorder );
else
setBorder( selectedWhiteBorder );
} else {
setBorder( null );
}
} else {
setText( value.toString() );
if( isSelected ) {
setBackground( list.getSelectionBackground() );
setForeground( list.getSelectionForeground() );
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
}
return this;
}
} // ColorCellRenderer
Figure 12.8 The custom Color class renderer.
|