Java Reference
In-Depth Information
If you don't like the default chooser panels, or you just want to add other color chooser panels
that work differently, you can create your own by subclassing the AbstractColorChooserPanel
class. To add a new panel to the existing set, call the following method:
public void addChooserPanel(AbstractColorChooserPanel panel)
If you later decide that you no longer want the new panel, you can remove it with this method:
public AbstractColorChooserPanel removeChooserPanel(AbstractColorChooserPanel panel)
To replace the existing set of panels, call this method:
setChooserPanels(AbstractColorChooserPanel panels[ ])
Creating a new panel entails subclassing AbstractColorChooserPanel and filling in the
details of choosing a color for the new panel. The class definition, shown in the following code
lines, includes five abstract methods. These five methods are what must be overridden.
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();
}
To demonstrate how to work with color chooser panels, let's look at how to create a new
one that displays a list of colors from the Color and SystemColor class. From this list, the user
must pick one. The panel will use a JComboBox to represent the list of colors. (The details of
using a JComboBox are explained in Chapter 13.) Figure 9-23 shows the finished panel. The panel
is created and added with the following source:
SystemColorChooserPanel newChooser = new SystemColorChooserPanel();
AbstractColorChooserPanel chooserPanels[] = {newChooser};
colorChooser.setChooserPanels(chooserPanels);
Search WWH ::




Custom Search