// set it up as the editor for all Color cells.
final ColorEditor colorEditor = new ColorEditor(button);
table.setDefaultEditor(Color.class, colorEditor);
// Set up the dialog that the button brings up.
final JColorChooser colorChooser = new JColorChooser();
// AbstractColorChooserPanel panels[] = colorChooser.getChooserPanels();
// The following has the effect of removing the RGB panel and making the HSB panel the default:
// AbstractColorChooserPanel preferredPanels[] = new AbstractColorChooserPanel[2];
// preferredPanels[0] = panels[1];
// preferredPanels[1] = panels[0];
// colorChooser.setChooserPanels(preferredPanels);
ActionListener okListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
colorEditor.currentColor = colorChooser.getColor();
Color newColor = colorEditor.currentColor;
StyleMapEntry entry = (StyleMapEntry) styleList.get(tbl.getSelectedRow());
int column = tbl.getSelectedColumn();
if (column == StyleConstants.BG_COLUMN)
entry.setBackground(newColor);
else
entry.setForeground(newColor);
tbl.repaint();
}
};
final JDialog dialog = JColorChooser.createDialog(button, "Pick a Color", true, colorChooser,
okListener, null);
// Here's the code that brings up the dialog.
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
button.setBackground(colorEditor.currentColor);
colorChooser.setColor(colorEditor.currentColor);
// Without the following line, the dialog comes up
// in the middle of the screen.
dialog.setLocationRelativeTo(button);
dialog.show();
}