Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8124964

ComboBox and ChoiceBox failed to update the displayed text after calling setItems from Platform.runLater

XMLWordPrintable

      As title. Below is a test case to reproduce the issue. See comments in the file for different test scenarios I've tried.

      package demo.combobox;

      import javafx.application.Application;
      import javafx.application.Platform;
      import javafx.collections.FXCollections;
      import javafx.geometry.Insets;
      import javafx.scene.Group;
      import javafx.scene.Scene;
      import javafx.scene.control.ChoiceBox;
      import javafx.scene.control.ComboBox;
      import javafx.scene.control.Label;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;

      public class ComboBoxSetItemBug {
          public static class ComboBoxSetItemsBug extends Application {

              public static void main(String[] args) {
                  launch(args);
              }

              @Override
              public void start(Stage stage) {
                  final ComboBox<String> comboBox = new ComboBox<>();
                  comboBox.setValue("Arial");

                  final ChoiceBox<String> choiceBox = new ChoiceBox<>();
                  choiceBox.setValue("Arial");

                  Runnable runnable = new Runnable() {
                      public void run() {
                          String value = comboBox.getValue();
                          System.out.println("(ComboBox) value before setItem: " + value);
                          comboBox.setItems(FXCollections.observableArrayList(getFontNames()));
                          System.out.println("(ComboBox) value after setItem: " + comboBox.getValue());

                          // I have to trick it - set the value to something else then set it back to make the "Arial" showing
                          // comboBox.setValue("");
                          // comboBox.setValue(item);
                          // System.out.println("(ComboBox) item after select: " + comboBox.getValue());

                          //////////////////////////////////

                          System.out.println("(ChoiceBox) value before setItems: " + choiceBox.getValue());
                          choiceBox.setItems(FXCollections.observableArrayList(getFontNames()));
                          System.out.println("(ChoiceBox) value after setItems: " + choiceBox.getValue());

                          // with choicebox, the same trick below doesn't work either
                          // choiceBox.setValue("");
                          // choiceBox.setValue(item);
                          // System.out.println("(ChoiceBox) value after select: " + choiceBox.getValue());
                      }
                  };
                  // if i run it directly without using runLater, the text shows just fine.
                  // runnable.run();
                  Platform.runLater(runnable);

                  VBox vbox = new VBox(6, comboBox, choiceBox, new Label("The selected value is Arial but the text is not shown on " +
                          "\nboth ComboBox and ChoiceBox. You can read comment " +
                          "\nin the file for more information"));
                  vbox.setPadding(new Insets(10));

                  Scene scene = new Scene(new Group());
                  stage.setTitle("ComboBox Bug");
                  ((Group) scene.getRoot()).getChildren().addAll(vbox);
                  stage.setScene(scene);
                  stage.show();
              }

              // partial font list
              public static String[] getFontNames() {
                  return new String[]{
                          "Agency FB",
                          "Aharoni",
                          "Algerian",
                          "Andalus",
                          "Angsana New",
                          "AngsanaUPC",
                          "Aparajita",
                          "Arabic Typesetting",
                          "Arial",
                          "Arial Black",
                          "Arial Narrow",
                          "Arial Rounded MT Bold",
                          "Arial Unicode MS",
                  };
              }
          }
      }

            psomashe Parvathi Somashekar (Inactive)
            dqiaojfx David Qiao (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: