Book Home Java Enterprise in a Nutshell Search this book

Chapter 25. The javax.swing.colorchooser Package

The classes and interfaces of the javax.swing.colorchooser package support the JColorChooser component of the javax.swing package and allow customization of that component. Figure 25-1 shows the class hierarchy of this package.


Figure 25-1. The javax.swing.colorchooser package

AbstractColorChooserPanelJava 1.2
javax.swing.colorchooserserializable accessible swing component

This abstract subclass of JPanel provides the basic framework for creating new color selection panels for the JColorChooser component. The default color selection panels provided by the JColorChooser are adequate for most applications. If you want to add a custom panel, you must subclass this class and provide implementations for the abstract methods. The buildChooser() method should create the GUI components that your color selection panel uses. getDisplayName(), getLargeDisplayIcon(), and getSmallDisplayIcon() should return a name and icons for your custom panel, respectively. These are displayed in the JTabbedPane tab for your color selection panel.

The most important task for your AbstractColorChooserPanel, however, is communicating with the ColorSelectionModel object of the JColorChooser. Call the getColorSelectionModel() method to obtain the model. When the user selects a color in your panel, you must change the selected color in the model, so that the JColorChooser knows of your selection. Conversely, if the user selects a color in some other panel, the abstract updateChooser() method is called to notify your panel of the change. Your panel should update itself to reflect the new color selection.

public abstract class AbstractColorChooserPanel extends JPanel {
// Public Constructors
public AbstractColorChooserPanel ();
// Property Accessor Methods (by property name)
public ColorSelectionModel getColorSelectionModel ();
public abstract String getDisplayName ();
public abstract Icon getLargeDisplayIcon ();
public abstract Icon getSmallDisplayIcon ();
// Public Instance Methods
public void installChooserPanel (JColorChooser enclosingChooser);
public void uninstallChooserPanel (JColorChooser enclosingChooser);
public abstract void updateChooser ();
// Public Methods Overriding JComponent
public void paint (java.awt.Graphics g);
// Protected Instance Methods
protected abstract void buildChooser ();
protected java.awt.Color getColorFromModel ();

Hierarchy: Object-->Component(java.awt.image.ImageObserver,java.awt.MenuContainer,Serializable)-->Container-->JComponent(Serializable)-->JPanel(Accessible)-->AbstractColorChooserPanel

Passed To: JColorChooser.{addChooserPanel(), removeChooserPanel(), setChooserPanels()}

Returned By: JColorChooser.{getChooserPanels(), removeChooserPanel()}, ColorChooserComponentFactory.getDefaultChooserPanels()

ColorChooserComponentFactoryJava 1.2

This class consists of static methods that return the individual color selection and color preview components that are used to create a default JColorChooser component. This class is used by the JColorChooser UI delegate object and normally is not used by applications. However, you may find it useful if you want to arrange standard color chooser components in a custom custom dialog box of your own.

public class ColorChooserComponentFactory {
// No Constructor
// Public Class Methods
public static AbstractColorChooserPanel[ ] getDefaultChooserPanels ();
public static JComponent getPreviewPanel ();
ColorSelectionModelJava 1.2

This interface defines the methods that must be implemented by an object that wants to keep track of a selected color for a JColorChooser. The interface is a simple one, consisting only of a pair of property accessor methods for the selected color and a pair of event registration methods for listeners that are interested in knowing when the selection changes. DefaultColorSelectionModel provides a simple implementation of this interface.

public abstract interface ColorSelectionModel {
// Event Registration Methods (by event name)
public abstract void addChangeListener (javax.swing.event.ChangeListener listener);
public abstract void removeChangeListener (javax.swing.event.ChangeListener listener);
// Public Instance Methods
public abstract java.awt.Color getSelectedColor ();
public abstract void setSelectedColor (java.awt.Color color);

Implementations: DefaultColorSelectionModel

Passed To: JColorChooser.{JColorChooser(), setSelectionModel()}

Returned By: JColorChooser.getSelectionModel(), AbstractColorChooserPanel.getColorSelectionModel()

DefaultColorSelectionModelJava 1.2
javax.swing.colorchooserserializable model

This class is a simple default implementation of the ColorSelectionModel interface.

public class DefaultColorSelectionModel implements ColorSelectionModel, Serializable {
// Public Constructors
public DefaultColorSelectionModel ();
public DefaultColorSelectionModel (java.awt.Color color);
// Event Registration Methods (by event name)
public void addChangeListener (javax.swing.event.ChangeListener l); Implements:ColorSelectionModel
public void removeChangeListener (javax.swing.event.ChangeListener l); Implements:ColorSelectionModel
// Methods Implementing ColorSelectionModel
public void addChangeListener (javax.swing.event.ChangeListener l);
public java.awt.Color getSelectedColor ();
public void removeChangeListener (javax.swing.event.ChangeListener l);
public void setSelectedColor (java.awt.Color color);
// Protected Instance Methods
protected void fireStateChanged ();
// Protected Instance Fields
protected transient javax.swing.event.ChangeEvent changeEvent ;
protected javax.swing.event.EventListenerList listenerList ;

Hierarchy: Object-->DefaultColorSelectionModel(ColorSelectionModel,Serializable)

Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.