Java Reference
In-Depth Information
return to the table model. The DefaultCellEditor offers a JComboBox for just this situation. After
configuring the ListCellRenderer for the JComboBox to display colors properly, you have a
TableCellEditor for picking colors. Figure 18-13 shows how this might appear.
Figure 18-13. JComboBox color editor
Tip Any time you can predefine all the choices, you can use the JComboBox as your editor through
DefaultCellEditor .
Listing 18-14 shows the class that represents the TableCellRenderer for the Color
column of the example shown in Figure 18-13 and the ListCellRenderer for the
JComboBox TableCellEditor . Because of the many similarities of the two renderer
components, their definitions are combined into one class.
Listing 18-14. JComboBox As Table Cell Renderer
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class ComboTableCellRenderer
implements ListCellRenderer, TableCellRenderer {
DefaultListCellRenderer listRenderer = new DefaultListCellRenderer();
DefaultTableCellRenderer tableRenderer = new DefaultTableCellRenderer();
private void configureRenderer(JLabel renderer, Object value) {
if ((value != null) && (value instanceof Color)) {
renderer.setIcon(new DiamondIcon((Color)value));
renderer.setText("");
} else {
renderer.setIcon(null);
renderer.setText((String)value);
}
}
 
Search WWH ::




Custom Search