Welcome to the user guide for the AdvancedColorChooser Swing Component. This short guide will help to get you started with this drop-in replacement for the standard Swing color chooser component, the JColorChooser.
The most common way to use the JColorChooser is as a popup dialog. The code for this might look a little like this:
Color newColor = JColorChooser.showDialog(myFrame, "Choose a color...", originalColor);
Using our AdvancedColorChooser in place of JColorChooser couldn't be simpler...
Color newColor = AdvancedColorChooser.showDialog(myFrame, "Choose a color", originalColor);
And that's really all there is to it. If the user cancels the color selection, a value of null will be returned. There is also an equivalent version of the "createDialog" method that gives you greater control over the actionListeners associated with the OK and Cancel buttons.
The JColorChooser can also be created as a standard component, to be embedded in any container. Well, the AdvancedColorChooser allows the same, with a few extra options.
![]()
An embedded color chooser
By default, the Swatches and Recent Colors panels will not be selectable - these options each display a popup window, the position of which is usually set relative to the Color Chooser dialog. If you are not using the dialog approach, you need to specify which dialog the popup panels will be positioned next to.
AdvancedColorChooser colorChooser = new AdvancedColorChooser(Color.red); colorChooser.setWindow(myDialog);
The AdvancedColorChooser ships with 3 sets of Swatch data - RGB, CMYK and X-Window. But it also supports the creation of custom Swatch sets.
To create a custom Swatch set, create a class that implements the SwatchData interface. Here is a simple example that creates a random Swatch set containing 30 random colors:
class RandomSwatchData implements SwatchData { Random r = new Random(System.currentTimeMillis()); public int[] getRawValues() { int[] rawValues = new int[90]; for (int i=0;i<90;i++) { rawValues[i] = r.nextInt(255); } return rawValues; } public String[] getNames() { return null; } public Dimension getDimension() { return new Dimension(6,5); } public String getTitle() { return "Random Swatch Data"; } }
The important things to note about this class are:
To register a custom Swatch set, use the following static method:
AdvancedColorChooser.addSwatchData(new RandomSwatchData());
There are also methods available to list all currently registered Swatches and remove individual Swatch sets. For example:
Enumeration swatches = AdvancedColorChooser.getSwatchData(); while (swatches.hasMoreElements()) { AdvancedColorChooser.removeSwatchData((SwatchData)swatches.nextElement()); }
The Recent Colors panel also features a set of 16 colors that can be stored as often used colors. To support the persistence of this data, we introduced the CustomColorDataSource interface. Here is a simple example of a custom color data source that creates random colors each time it is called:
class RandomColorDataSource implements CustomColorDataSource { Random r = new Random(System.currentTimeMillis()); public Color getColor(int i) { return new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)); } public void setColor(int i,Color c) { } }
Data sources could also get (and put) the custom color data from a properties file, a network server, Windows Registry, etc. There are two methods for associating a data source with the AdvancedColorChooser, depending on the type of chooser you are using (embedded or dialog).
For a dialog based color chooser the instance of the AdvancedColorChooser is not available to the programmer. So, we use a static method to register a data source before the chooser is created:
AdvancedColorChooser.registerCustomColorDataSource(new RandomColorDataSource()); Color c = AdvancedColorChooser.showDialog(myFrame,"Choose a color",Color.white);
If you are using an embedded color chooser you should use the setDataSource method:
AdvancedColorChooser colorChooser = new AdvancedColorChooser(Color.red); colorChooser.setDataSource(new RandomColorDataSource());
Yes, the AdvancedColorChooser has been created as a JavaBean, making it really easy to use in conjunction with your favourite Java IDE. All you need to do is import the AdvancedColorChooser JAR file into the IDE, and it should become available in the component palette of your choice. Please refer to the instruction manual for your IDE for more details on using and importing JavaBeans.
I hope you enjoy using the AdvancedColorChooser - if you need any help using this or any of our components, please email us: colorchooser@sygem.com