Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

ColorEditor.java

00001 /* 
00002  * ColorEditor.java 
00003  */
00004 package DisplayGUI;
00005 
00006 import javax.swing.AbstractCellEditor;
00007 import javax.swing.table.TableCellEditor;
00008 import javax.swing.JButton;
00009 import javax.swing.JColorChooser;
00010 import javax.swing.JDialog;
00011 import javax.swing.JTable;
00012 import java.awt.Color;
00013 import java.awt.Component;
00014 import java.awt.event.ActionEvent;
00015 import java.awt.event.ActionListener;
00016 
00017 public class ColorEditor extends AbstractCellEditor
00018                          implements TableCellEditor,
00019                                     ActionListener {
00020     Color currentColor;
00021     JButton button;
00022     JColorChooser colorChooser;
00023     JDialog dialog;
00024     protected static final String EDIT = "edit";
00025 
00026     public ColorEditor() {
00027         //Set up the editor (from the table's point of view),
00028         //which is a button.
00029         //This button brings up the color chooser dialog,
00030         //which is the editor from the user's point of view.
00031         button = new JButton();
00032         button.setActionCommand(EDIT);
00033         button.addActionListener(this);
00034         button.setBorderPainted(false);
00035 
00036         //Set up the dialog that the button brings up.
00037         colorChooser = new JColorChooser();
00038         dialog = JColorChooser.createDialog(button,
00039                                         "Pick a Color",
00040                                         true,  //modal
00041                                         colorChooser,
00042                                         this,  //OK button handler
00043                                         null); //no CANCEL button handler
00044     }
00045 
00050     public void actionPerformed(ActionEvent e) {
00051         if (EDIT.equals(e.getActionCommand())) {
00052             //The user has clicked the cell, so
00053             //bring up the dialog.
00054             button.setBackground(currentColor);
00055             colorChooser.setColor(currentColor);
00056             dialog.setVisible(true);
00057 
00058             //Make the renderer reappear.
00059             fireEditingStopped();
00060 
00061         } else { //User pressed dialog's "OK" button.
00062             currentColor = colorChooser.getColor();
00063         }
00064     }
00065 
00066     //Implement the one CellEditor method that AbstractCellEditor doesn't.
00067     public Object getCellEditorValue() {
00068         return currentColor;
00069     }
00070 
00071     //Implement the one method defined by TableCellEditor.
00072     public Component getTableCellEditorComponent(JTable table,
00073                                                  Object value,
00074                                                  boolean isSelected,
00075                                                  int row,
00076                                                  int column) {
00077         currentColor = (Color)value;
00078         return button;
00079     }
00080 }
00081 

Generated on Thu Jul 15 09:55:40 2004 for SCT DAQ/DCS Software - Java by doxygen 1.3.5