| 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 EDIT = "edit"; 20 21 public ColorCellEditor() { 22 button = new JButton(); 23 button.setActionCommand(EDIT); 24 button.addActionListener(this); 25 button.setBorderPainted(false); 26 27 colorChooser = new JColorChooser(); 29 dialog = JColorChooser.createDialog(button, 30 "Pick a Color", 31 true, colorChooser, 33 this, null); } 36 37 public void actionPerformed(ActionEvent e) { 38 if (EDIT.equals(e.getActionCommand())) { 39 button.setBackground(currentColor); 42 colorChooser.setColor(currentColor); 43 dialog.setVisible(true); 44 45 fireEditingStopped(); 47 } else { currentColor = colorChooser.getColor(); 49 } 50 } 51 52 public Object getCellEditorValue() { 54 return currentColor; 55 } 56 57 public Component getTableCellEditorComponent(JTable table, 59 Object 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 |