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

[ComboBox] Setting items in and editable ComboBox clears the editor text under some circumstances

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • emb-8u6, 8u20
    • 8
    • javafx
    • java version "1.8.0-ea"
      Java(TM) SE Runtime Environment (build 1.8.0-ea-b119)
      Java HotSpot(TM) 64-Bit Server VM (build 25.0-b61, mixed mode)

      Sample app below. Select an item and click the buttons that resets the items property.

      Nothing about this behavior is mentioned in the documentation. Also its totally unnecessary as the documentations tells:
      http://docs.oracle.com/javafx/2/ui_controls/combo-box.htm
      that the value doesn't have to be among items.

      {code}
      import javafx.application.Application;
      import javafx.collections.FXCollections;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.ComboBox;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;
      import javafx.util.StringConverter;

      import java.util.HashMap;
      import java.util.Map;

      public class LookupApp extends Application {

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

          @Override
          public void start(Stage primaryStage) throws Exception {
              
              final Map<String,SomeBean> map = new HashMap<>();
              map.put("aabbaa", new SomeBean("aabbaa"));
              map.put("bbc", new SomeBean("bbc"));
              final ComboBox<SomeBean> combo = new ComboBox<>();
              combo.setEditable(true);
              combo.setItems(FXCollections.observableArrayList(map.values()));
              combo.setConverter(new StringConverter<LookupApp.SomeBean>() {
                  
                  @Override
                  public String toString(SomeBean bean) {
                      if(bean != null) {
                          return bean.getId();
                      } else {
                          return "";
                      }
                  }
                  
                  @Override
                  public SomeBean fromString(String text) {
                      return map.get(text);
                  }
              });
              Button button = new Button("Refresh items");
              button.setOnAction(new EventHandler<ActionEvent>() {
                  
                  @Override
                  public void handle(ActionEvent arg0) {
                      combo.setItems(FXCollections.observableArrayList(map.values()));
                  }
              });
              primaryStage.setScene(new Scene(new VBox(combo, button)));
              primaryStage.show();
          }

          public class SomeBean {
              
              private String id;
              
              public SomeBean(String id) {
                  this.id = id;
              }
              
              public String getId() {
                  return id;
              }
          }
      }
      {code}

            jgiles Jonathan Giles
            awalczakijfx Adam Walczak (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: