01: package snow.sortabletable;
02:
03: import java.util.*;
04: import java.awt.*;
05: import java.awt.color.*;
06: import java.awt.event.*;
07: import javax.swing.*;
08: import javax.swing.event.*;
09: import javax.swing.table.*;
10:
11: public class ColorCellEditor extends AbstractCellEditor implements
12: TableCellEditor, ActionListener {
13: Color currentColor;
14: JButton button;
15: JColorChooser colorChooser;
16: JDialog dialog;
17: protected static final String EDIT = "edit";
18:
19: public ColorCellEditor() {
20: button = new JButton();
21: button.setActionCommand(EDIT);
22: button.addActionListener(this );
23: button.setBorderPainted(false);
24:
25: //Set up the dialog that the button brings up.
26: colorChooser = new JColorChooser();
27: dialog = JColorChooser.createDialog(button, "Pick a Color",
28: true, //modal
29: colorChooser, this , //OK button handler
30: null); //no CANCEL button handler
31: }
32:
33: public void actionPerformed(ActionEvent e) {
34: if (EDIT.equals(e.getActionCommand())) {
35: //The user has clicked the cell, so
36: //bring up the dialog.
37: button.setBackground(currentColor);
38: colorChooser.setColor(currentColor);
39: dialog.setVisible(true);
40:
41: fireEditingStopped(); //Make the renderer reappear.
42:
43: } else { //User pressed dialog's "OK" button.
44: currentColor = colorChooser.getColor();
45: }
46: }
47:
48: //Implement the one CellEditor method that AbstractCellEditor doesn't.
49: public Object getCellEditorValue() {
50: return currentColor;
51: }
52:
53: //Implement the one method defined by TableCellEditor.
54: public Component getTableCellEditorComponent(JTable table,
55: Object value, boolean isSelected, int row, int column) {
56: if (value instanceof Color) {
57: currentColor = (Color) value;
58: }
59: return button;
60: }
61: }
|