Product \ ColorSelection

main API Code Example

Code Examples for the ColorSelection widget

The ColorChooser is a widget that can be used inside an already existing window or you can ask it to show itself in a new dialog directly. Using one of the static methods to show the chooser in a new dialog allows you to request a new color with just one call:

Color newColor = ColorSelection.selectColorDialog(
    (Window) SwingUtilities.getWindowAncestor(this), oldColor);

A convinient usage of the color selector is by providing the UICColorButton on your GUI. The button will show the current color and pressing the button will show the selection dialog plus change the current color of the button.

    bSelectColor = new UICColorButton();
    myPanel.add(bSelectColor);
    bSelectColor.setColor(color);

Whenever a user presses the button, and actually selects a new color (by pressing the OK button) the color is changed and the colorButton will fire a propertyChange signal. You can listen to the changes like this:

    bSelectColor.addPropertyChangeListener(UICColorButton.SIGNALNAME,
        new PropertyChangeListener() {
          public void propertyChange(PropertyChangeEvent evt) {
            Color newColor = (Color) evt.getNewValue();
            // do something with color
          }
    });
The "newColorPressed" method will be called whenever the color changed.

Note that the former listener is the normal Swing method; in UICollection we propose an action framework that makes connections a lot simpler to maintain, but also a lot more robust and thread-safe. We therefor suggest to write the following instead:

    new PropertyChangedAction(this, "colorChangedSlot")
        .add(UICColorButton.SIGNALNAME, bSelectColor);

    private void colorChangedSlot(Color newColor) {
        // do something with color
    }