4 Replies Latest reply on Jan 18, 2013 9:15 PM by ibibby

    setValue() doesn't work correctly on a ColorPicker

      I have a ColorPicker control in my app. The color can be set directly by the user, but can also be set programmatically by the app (for instance, when the user undoes their action of changing color).

      Unfortunately, setValue() doesn't work properly. If I call setValue() before the user has clicked on the ColorPicker for the first time, it will work. However, if I call it after the user has used the control once, setValue() will fail to change the visibly selected color. It will change the underlying selection, so if I call getValue() after setValue() it will return the new value, but the visible selection on the control doesn't change.

      First of all, is this a bug? And secondly, is there any workaround to make the control update itself to show the proper color after a setValue()?

      Edited by: ibibby on Jan 18, 2013 12:20 PM
        • 1. Re: setValue() doesn't work correctly on a ColorPicker
          I swear this came up before on this forum, but I can't find it now...

          Anyway, I came up with this (ugly) workaround:
          import javafx.application.Application;
          import javafx.event.ActionEvent;
          import javafx.event.EventHandler;
          import javafx.geometry.Insets;
          import javafx.scene.Scene;
          import javafx.scene.control.Button;
          import javafx.scene.control.ColorPicker;
          import javafx.scene.layout.StackPane;
          import javafx.scene.layout.VBox;
          import javafx.scene.paint.Color;
          import javafx.stage.Stage;
          public class ColorPickerChangeColor extends Application {
            public void start(Stage stage) throws Exception {
              VBox vbox = new VBox();
              vbox.setPadding(new Insets(10));
              final Button button = new Button("Press me.");
              final ColorPicker colorpicker = new ColorPicker(Color.BLUE);
              button.setOnAction(new EventHandler<ActionEvent>() {
                public void handle(ActionEvent arg0) {
                  // work around to update colorpicker's displayed selection:
                  colorpicker.fireEvent(new ActionEvent(button, colorpicker));
              StackPane root = new StackPane();
              Scene scene = new Scene(root, 200, 200);
            public static void main(String[] args) {
          • 2. Re: setValue() doesn't work correctly on a ColorPicker
            For JavaFX 2.2, there are many bugs in ColorPicker, check the issue tracker and see if yours is listed:
            I think the ColorPicker in JavaFX 2.2 is buggy enough that I wouldn't recommend it's use.

            Here is a (really) basic alternate implementation you could use as a starting point if you wanted to have your own implementation for now:
            • 3. Re: setValue() doesn't work correctly on a ColorPicker
              Thanks, James, that worked for me.

              I know the control is buggy, but it suffices for my purposes, so I think I'll stick with it for now.
              • 4. Re: setValue() doesn't work correctly on a ColorPicker
                Btw, this appears to be the JIRA ticket for this issue, in case anyone is interested: http://javafx-jira.kenai.com/browse/RT-26633