KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > sortabletable > ColorCellEditor


1 package snow.sortabletable;
2
3 import java.util.*;
4 import java.awt.*;
5 import java.awt.color.*;
6 import java.awt.event.*;
7 import javax.swing.*;
8 import javax.swing.event.*;
9 import javax.swing.table.*;
10
11
12 public final class ColorCellEditor extends AbstractCellEditor
13                          implements TableCellEditor,
14                                     ActionListener {
15     Color currentColor;
16     JButton button;
17     JColorChooser colorChooser;
18     JDialog dialog;
19     protected static final String JavaDoc EDIT = "edit";
20
21     public ColorCellEditor() {
22         button = new JButton();
23         button.setActionCommand(EDIT);
24         button.addActionListener(this);
25         button.setBorderPainted(false);
26
27         //Set up the dialog that the button brings up.
28
colorChooser = new JColorChooser();
29         dialog = JColorChooser.createDialog(button,
30                                         "Pick a Color",
31                                         true, //modal
32
colorChooser,
33                                         this, //OK button handler
34
null); //no CANCEL button handler
35
}
36
37     public void actionPerformed(ActionEvent e) {
38         if (EDIT.equals(e.getActionCommand())) {
39             //The user has clicked the cell, so
40
//bring up the dialog.
41
button.setBackground(currentColor);
42             colorChooser.setColor(currentColor);
43             dialog.setVisible(true);
44
45             fireEditingStopped(); //Make the renderer reappear.
46

47         } else { //User pressed dialog's "OK" button.
48
currentColor = colorChooser.getColor();
49         }
50     }
51
52     //Implement the one CellEditor method that AbstractCellEditor doesn't.
53
public Object JavaDoc getCellEditorValue() {
54         return currentColor;
55     }
56
57     //Implement the one method defined by TableCellEditor.
58
public Component getTableCellEditorComponent(JTable table,
59                                                  Object JavaDoc value,
60                                                  boolean isSelected,
61                                                  int row,
62                                                  int column)
63     {
64         if(value instanceof Color)
65         {
66           currentColor = (Color) value;
67         }
68         return button;
69     }
70 }
Popular Tags