ChoiceBox docs state "it is possible for the developer to specify the selected item to be something other than what is available in the predefined list".
Example code:
public class ChoiceTest extends Application {
@Override
public void start(Stage stage) throws Exception {
ObservableList<String> list = FXCollections.observableArrayList("A", "B", "C");
ChoiceBox<String> choiceBox = new ChoiceBox<>();
choiceBox.getSelectionModel().select("D"); // or choiceBox.setValue
choiceBox.setItems(list);
stage.setScene(new Scene(choiceBox));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
The result is that the displayed item is blank. A quick look shows that ChoiceBoxSkin is set to display "" when selectedIndex = -1, which is the value for a selected item which is not in the list of items.
Example code:
public class ChoiceTest extends Application {
@Override
public void start(Stage stage) throws Exception {
ObservableList<String> list = FXCollections.observableArrayList("A", "B", "C");
ChoiceBox<String> choiceBox = new ChoiceBox<>();
choiceBox.getSelectionModel().select("D"); // or choiceBox.setValue
choiceBox.setItems(list);
stage.setScene(new Scene(choiceBox));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
The result is that the displayed item is blank. A quick look shows that ChoiceBoxSkin is set to display "" when selectedIndex = -1, which is the value for a selected item which is not in the list of items.
- duplicates
-
JDK-8087555 [ChoiceBox] uncontained value not shown
- Resolved