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

[ChoiceBox] uncontained value not shown

    XMLWordPrintable

    Details

      Description

      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
              //box.setValue(items.get(0));
              Button setSelectedItemUncontained = new Button("Set selectedItem to uncontained");
              setSelectedItemUncontained.setOnAction(e -> {
                  SingleSelectionModel<String> model = box.getSelectionModel();
                  model.select("myDummySelectedItem");
                  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();
                  box.setValue("myDummyValue");
                  LOG.info("selected/item/value" + model.getSelectedIndex()
                          + "/" + model.getSelectedItem() + "/" + box.getValue());
              });
              HBox buttons = new HBox(setSelectedItemUncontained, setValue);
              
              BorderPane pane = new BorderPane(box);
              pane.setBottom(buttons);
              return pane;
          }

          @Override
          public void start(Stage primaryStage) throws Exception {
              Scene scene = new Scene(getContent());
              primaryStage.setScene(scene);
              primaryStage.show();

          }

          public static void main(String[] args) {
              launch();
          }
          
          @SuppressWarnings("unused")
          private static final Logger LOG = Logger.getLogger(ChoiceBoxUncontainedValue.class
                  .getName());
      }

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              fastegal Jeanette Winzenburg
              Reporter:
              fastegal Jeanette Winzenburg
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported: