Forum Stats

  • 3,767,863 Users
  • 2,252,726 Discussions
  • 7,874,367 Comments

Discussions

setValue() doesn't work correctly on a ColorPicker

ibibby
ibibby Member Posts: 67
edited Jan 18, 2013 4:15PM in JavaFX 2.0 and Later
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
drenda81Sahil Huseynzade

Answers

  • James_D
    James_D Member Posts: 1,496 Gold Trophy
    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);
      }
    
    }
    drenda81Sahil Huseynzade
  • jsmith
    jsmith Member Posts: 2,856
    edited Jan 18, 2013 3:33PM
    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
  • ibibby
    ibibby Member Posts: 67
    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.
  • ibibby
    ibibby Member Posts: 67
    Btw, this appears to be the JIRA ticket for this issue, in case anyone is interested: http://javafx-jira.kenai.com/browse/RT-26633
This discussion has been closed.