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

[ChoiceBox] uncontained value not shown




      ChoiceBox's explicitly allow selection of an item that's not contained in the backing list. Value/SelectedItem are returning the external item, but it is not shown in the box's label (it's empty). Below is an example demonstrating the problem.

      Culprit is ChoiceBoxSkin that clears the label if the selectedIndex < 0 (done at several places)

      package de.swingempire.fx.scene.control.selection;

      import java.util.logging.Logger;

      import javafx.application.Application;
      import javafx.collections.FXCollections;
      import javafx.collections.ObservableList;
      import javafx.scene.Parent;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.ChoiceBox;
      import javafx.scene.control.SingleSelectionModel;
      import javafx.scene.layout.BorderPane;
      import javafx.scene.layout.HBox;
      import javafx.stage.Stage;

       * Issue: uncontained value not shown in choiceBox's label.
       * To reproduce:
       * - run example and click any of the buttons to select/setValue to an uncontained
       * item
       * - expected: uncontained item shown
       * - actual: uncontained item not shown
       * Additionally: behaviour different for initial selection/empty selectiono
       * initially selected: set uncontained value/selectedItem - > selection is cleared, empty
       * choiceBox, no selection marker in popup (expected behaviour)
       * initially unselected, select by clicking into drop-down: set uncontained value/selectedItem
       * -> old selection shown in choicebox, old selection marker in popup (bug)
       * @author Jeanette Winzenburg, Berlin
      public class ChoiceBoxUncontainedValue extends Application {

          ObservableList<String> items = FXCollections.observableArrayList(
                  "5-item", "4-item", "3-item", "2-item", "1-item");

           * @return
          private Parent getContent() {
              ChoiceBox<String> box = new ChoiceBox<>(items);
              // variant: initial uncontained value
              //box.setValue("initial uncontained");
              // variant: initial selection
              Button setSelectedItemUncontained = new Button("Set selectedItem to uncontained");
              setSelectedItemUncontained.setOnAction(e -> {
                  SingleSelectionModel<String> model = box.getSelectionModel();
                  LOG.info("selected/item/value" + model.getSelectedIndex()
                          + "/" + model.getSelectedItem() + "/" + box.getValue());
              Button setValue = new Button("Set value to uncontained");
              setValue.setOnAction(e -> {
                  SingleSelectionModel<String> model = box.getSelectionModel();
                  LOG.info("selected/item/value" + model.getSelectedIndex()
                          + "/" + model.getSelectedItem() + "/" + box.getValue());
              HBox buttons = new HBox(setSelectedItemUncontained, setValue);
              BorderPane pane = new BorderPane(box);
              return pane;

          public void start(Stage primaryStage) throws Exception {
              Scene scene = new Scene(getContent());


          public static void main(String[] args) {
          private static final Logger LOG = Logger.getLogger(ChoiceBoxUncontainedValue.class


        Issue Links



              fastegal Jeanette Winzenburg
              fastegal Jeanette Winzenburg
              0 Vote for this issue
              4 Start watching this issue