Changing the Color Chooser Panels : JColorChooser « Swing « Java Tutorial






  1. Create your own by subclassing the AbstractColorChooserPanel class.
  2. To add a new panel to the existing set, call: public void addChooserPanel(AbstractColorChooserPanel panel)
  3. To remove it: public AbstractColorChooserPanel removeChooserPanel(AbstractColorChooserPanel panel)
  4. To replace the existing set of panels: setChooserPanels(AbstractColorChooserPanel panels[ ])
public abstract class AbstractColorChooserPanel extends JPanel {
  public AbstractColorChooserPanel();
  protected abstract void buildChooser();
  protected Color getColorFromModel();
  public ColorSelectionModel getColorSelectionModel();
  public int getDisplayMnemonicIndex();
  public abstract String getDisplayName();
  public abstract Icon getLargeDisplayIcon();
  public int getMnemonic();
  public abstract Icon getSmallDisplayIcon();
  public void installChooserPanel(JColorChooser);
  public void paint(Graphics);
  public void uninstallChooserPanel(JColorChooser);
  public abstract void updateChooser();
}
Changing the Color Chooser Panels
import java.awt.Color;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.colorchooser.AbstractColorChooserPanel;
import javax.swing.colorchooser.ColorSelectionModel;

class SystemColorChooserPanel
    extends AbstractColorChooserPanel
    implements ItemListener {
  private static int NOT_FOUND = -1;

  JComboBox comboBox;
  String labels[] = {
    "BLACK",
    "BLUE",
    "CYAN",
    "<Custom>"};
  Color colors[] = {
    Color.BLACK,
    Color.BLUE,
    Color.CYAN,
    null};

  private void setColor(Color newColor) {
    int position = findColorPosition(newColor);
    comboBox.setSelectedIndex(position);
  }

  private int findColorLabel(Object label) {
    String stringLabel = label.toString();
    int position = NOT_FOUND;
    for (int i=0,n=labels.length; i<n; i++) {
      if (stringLabel.equals(labels[i])) {
        position=i;
        break;
      }
    }
    return position;
  }

  private int findColorPosition(Color color) {
    int position = colors.length-1;

    int colorRGB = color.getRGB();
    for (int i=0,n=colors.length; i<n; i++) {
      if ((colors[i] != null) && (colorRGB == colors[i].getRGB())) {
        position=i;
        break;
      }
    }
    return position;
  }

  public void itemStateChanged(ItemEvent itemEvent) {
    int state = itemEvent.getStateChange();
    if (state == ItemEvent.SELECTED) {
      int position = findColorLabel(itemEvent.getItem());

      if ((position != NOT_FOUND) && (position != labels.length-1)) {
        ColorSelectionModel selectionModel = getColorSelectionModel();
        selectionModel.setSelectedColor(colors[position]);
      }
    }
  }

  public String getDisplayName() {
    return "SystemColor";
  }
  public Icon getSmallDisplayIcon() {
    return new ImageIcon("yourFile.gif");
  }

  public Icon getLargeDisplayIcon() {
    return new ImageIcon("yourFile.gif");
  }

  protected void buildChooser() {
    comboBox = new JComboBox(labels);
    comboBox.addItemListener(this);
    add(comboBox);
  }

  public void updateChooser() {
    Color color = getColorFromModel();
    setColor(color);
  }
}


public class JColorChooserWithCustomSelectionPanel {

  public static void main(String[] a) {

    JColorChooser colorChooser = new JColorChooser();
    colorChooser.addChooserPanel(new SystemColorChooserPanel());

    JDialog d = colorChooser.createDialog(null,"",true,colorChooser,null,null);
    
    d.setVisible(true);
  }

}








14.76.JColorChooser
14.76.1.Display Color chooser dialogDisplay Color chooser dialog
14.76.2.Use a Color Chooser
14.76.3.Creating a JColorChooser Dialog
14.76.4.Getting and Setting the Selected Color in a JColorChooser Dialog
14.76.5.Preview pane simply displays the currently selected color.
14.76.6.Listening to Color Selection ChangesListening to Color Selection Changes
14.76.7.Creating and Showing a JColorChooser Pop-Up WindowCreating and Showing a JColorChooser Pop-Up Window
14.76.8.Customizing Action Listeners on JColorChooser ButtonsCustomizing Action Listeners on JColorChooser Buttons
14.76.9.Linking JColorChooser with component's colorLinking JColorChooser with component's color
14.76.10.Dragging-and-Dropping Colors Across JColorChooser ComponentsDragging-and-Dropping Colors Across JColorChooser Components
14.76.11.JColorChooser with custom preview panel
14.76.12.Changing the Color Chooser PanelsChanging the Color Chooser Panels
14.76.13.Setting the Order of the Color Chooser Panel Tabs in a JColorChooser Dialog
14.76.14.Retrieving the Color Chooser Panels in a JColorChooser Dialog
14.76.15.Removing a Color Chooser Panel from a JColorChooser Dialog
14.76.16.Removing the Preview Panel from a JColorChooser Dialog
14.76.17.Customizing the Preview Panel of a JColorChooser Dialog
14.76.18.Listening for OK and Cancel Events in a JColorChooser Dialog
14.76.19.Adding a Custom Color Chooser Panel to a JColorChooser Dialog
14.76.20.Customizing a JColorChooser Look and Feel
14.76.21.Choose foreground or background color