This discussion is archived
4 Replies Latest reply: Jan 18, 2013 1:15 PM by ibibby RSS

setValue() doesn't work correctly on a ColorPicker

ibibby Newbie
Currently Being Moderated
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
    James_D Guru
    Currently Being Moderated
    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 {
     
      @Override
      public void start(Stage stage) throws Exception {
     
        VBox vbox = new VBox();
        vbox.setPadding(new Insets(10));
        vbox.setSpacing(10);
        final Button button = new Button("Press me.");
        final ColorPicker colorpicker = new ColorPicker(Color.BLUE);
     
        button.setOnAction(new EventHandler<ActionEvent>() {
          @Override
          public void handle(ActionEvent arg0) {
            colorpicker.setValue(Color.YELLOW);
            
            // work around to update colorpicker's displayed selection:
            colorpicker.fireEvent(new ActionEvent(button, colorpicker));
          }
        });
     
        vbox.getChildren().add(button);
        vbox.getChildren().add(colorpicker);
     
        StackPane root = new StackPane();
     
        root.getChildren().add(vbox);
        stage.setTitle("ColorPicker");
        Scene scene = new Scene(root, 200, 200);
     
        stage.setScene(scene);
        stage.show();
     
      }
    
      public static void main(String[] args) {
        launch(args);
      }
    
    }
  • 2. Re: setValue() doesn't work correctly on a ColorPicker
    jsmith Guru
    Currently Being Moderated
    For JavaFX 2.2, there are many bugs in ColorPicker, check the issue tracker and see if yours is listed:
    http://javafx-jira.kenai.com/secure/IssueNavigator.jspa?reset=true&jqlQuery=summary+~+colorpicker+OR+description+~+colorpicker+OR+comment+~+colorpicker
    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:
    https://gist.github.com/2030464
  • 3. Re: setValue() doesn't work correctly on a ColorPicker
    ibibby Newbie
    Currently Being Moderated
    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
    ibibby Newbie
    Currently Being Moderated
    Btw, this appears to be the JIRA ticket for this issue, in case anyone is interested: http://javafx-jira.kenai.com/browse/RT-26633

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points