This discussion is archived
2 Replies Latest reply: Nov 15, 2012 12:44 AM by 973809 RSS

Rendering problem when setting ChoiceBox items with a service

973809 Newbie
Currently Being Moderated
I'm trying to prepare some examples to show various FX features when I came across this strange issue. I'm using a javafx.concurrent.Service to populate the items of a ChoiceBox. When I click the ChoiceBox to show the list of choices I only get an empty rectangle with a triangle drawn on top. It's only when I click on the triangle that the ChoiceBox gets redrawn properly. From what I could see in the debugger is that the items are there at the time when I click the ChoiceBox.
final ChoiceBox<String> choiceBox = new ChoiceBox<>();
ExampleService service = new ExampleService();
service.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
     @Override
     public void handle(WorkerStateEvent aEvent) {
          ObservableList<String> tempValue = (ObservableList<String>) aEvent.getSource().getValue();
          choiceBox.setItems(tempValue);
     }
});
service.start();
Screenshot: [url https://dl.dropbox.com/u/8978331/ChoiceBoxRenderingProblem.png]https://dl.dropbox.com/u/8978331/ChoiceBoxRenderingProblem.png
Sample code: [url https://dl.dropbox.com/u/8978331/Example2App.java]https://dl.dropbox.com/u/8978331/Example2App.java

Shall I write a Jira issue for that one or is it my mistake?

I'm using 1.7.0_10-ea-b13 on Windows 7 64bit but same issue occurs on Mac OS X.

(PS: I could also use binding to accomplish this task but unfortunately RT-22449 prevents me from doing so.)

Edited by: Thilo on Nov 13, 2012 10:57 AM to fix the broken links
  • 1. Re: Rendering problem when setting ChoiceBox items with a service
    James_D Guru
    Currently Being Moderated
    Looks like a bug to me. It's not related to using a service (or multithreading): the code below suffers from the same issue.
    import javafx.application.Application;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.ChoiceBox;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    
    
    public class ChoiceBoxTest extends Application {
    
      @Override
      public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Example 3");
    
        final ChoiceBox<String> choiceBox = new ChoiceBox<>();
        final Button populateButton = new Button("Populate");
        populateButton.setOnAction(new EventHandler<ActionEvent>() {
    
          @Override
          public void handle(ActionEvent event) {
            ObservableList<String> tempValue = FXCollections.observableArrayList();
            for (int i = 0; i < 10; i++) {
              tempValue.add("Item " + i);
            }
            choiceBox.setItems(tempValue);
            // This will work:
            // choiceBox.getItems().setAll(tempValue);
          }
          
        });
    
        VBox root = new VBox();
        root.getChildren().addAll(populateButton, choiceBox);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();    
      }
    
      public static void main(String[] args) {
        launch(args);
      }
    
    }
    For a workaround, replace
    choiceBox.setItems(tempValue);
    with
    choiceBox.getItems().setAll(tempValue);
  • 2. Re: Rendering problem when setting ChoiceBox items with a service
    973809 Newbie
    Currently Being Moderated
    Thanks for the work-around. I've filed RT-26273 for this one now.

Legend

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