Class ColorPicker

  • All Implemented Interfaces:
    Styleable, EventTarget, Skinnable


    public class ColorPicker
    extends ComboBoxBase<Color>

    ColorPicker control allows the user to select a color from either a standard palette of colors with a simple one click selection OR define their own custom color.

    The value is the currently selected Color. An initial color can be set by calling setColor or via the constructor. If nothing is specified, a default initial color is used.

    The ColorPicker control provides a color palette with a predefined set of colors. If the user does not want to choose from the predefined set, they can create a custom color by interacting with a custom color dialog. This dialog provides RGB, HSB and Web modes of interaction, to create new colors. It also lets the opacity of the color to be modified.

    Once a new color is defined, users can choose whether they want to save it or just use it. If the new color is saved, this color will then appear in the custom colors area on the color palette. Also getCustomColors returns the list of saved custom colors.

    The promptText is not supported and hence is a no-op. But it may be supported in the future.

    
     final ColorPicker colorPicker = new ColorPicker();
     colorPicker.setOnAction(new EventHandler() {
         public void handle(Event t) {
             Color c = colorPicker.getValue();
             System.out.println("New Color's RGB = "+c.getRed()+" "+c.getGreen()+" "+c.getBlue());
         }
     });
     

    The ColorPicker control's appearance can be styled in three ways: a simple Button mode, MenuButton mode or SplitMenuButton mode. The default is MenuButton mode. For a Button like appearance the style class to use is STYLE_CLASS_BUTTON and for SplitMenuButton appearance and behavior, the style class to use is STYLE_CLASS_SPLIT_BUTTON.

    
     colorPicker.getStyleClass().add("button");
     
    or
    
     colorPicker.getStyleClass().add("split-button");
     
    Since:
    JavaFX 2.2
    • Field Detail

      • STYLE_CLASS_BUTTON

        public static final String STYLE_CLASS_BUTTON
        The style class to specify a Button like appearance of ColorPicker control.
        See Also:
        Constant Field Values
      • STYLE_CLASS_SPLIT_BUTTON

        public static final String STYLE_CLASS_SPLIT_BUTTON
        The style class to specify a SplitMenuButton like appearance of ColorPicker control.
        See Also:
        Constant Field Values
    • Constructor Detail

      • ColorPicker

        public ColorPicker​()
        Creates a default ColorPicker instance with a selected color set to white.
      • ColorPicker

        public ColorPicker​(Color color)
        Creates a ColorPicker instance and sets the selected color to the given color.
        Parameters:
        color - to be set as the currently selected color of the ColorPicker.
    • Method Detail

      • getCustomColors

        public final ObservableList<Color> getCustomColors​()
        Gets the list of custom colors added to the Color Palette by the user.
        Returns:
        the list of custom colors
      • createDefaultSkin

        protected Skin<?> createDefaultSkin​()
        Create a new instance of the default skin for this control. This is called to create a skin for the control if no skin is provided via CSS -fx-skin or set explicitly in a sub-class with setSkin(...).
        Overrides:
        createDefaultSkin in class Control
        Returns:
        new instance of default skin for this control. If null then the control will have no skin unless one is provided by css.