javafx 中combobox设置成combobox.setEditable(true);combobox就无法选择,也获取不到选择了的值。请问有遇到这种情况的嘛?
下面是代码
private ComboBox peopleComBox; private ObservableList peopleComBoxData = FXCollections.observableArrayList(); public MainPageController() { peopleComBoxData.add(new SimpleStringProperty("HansMuster")); peopleComBoxData.add(new SimpleStringProperty("RuthMueller")); peopleComBoxData.add(new SimpleStringProperty("HeinzKurz")); } private void initialize() { peopleComBox.setItems(peopleComBoxData); peopleComBox.setEditable(true); // Define rendering of the list of values in ComboBox drop down. peopleComBox.setCellFactory((comboBox) -> { return new ListCell() { @Override protected void updateItem(StringProperty item, boolean empty) { super.updateItem(item, empty); if (item == null || empty) { setText(null); } else { setText(item.get()); } } }; }); // Define rendering of selected value shown in ComboBox. peopleComBox.setConverter(new StringConverter() { @Override public String toString(StringProperty person) { if (person == null) { return null; } else { return person.get(); } } @Override public StringProperty fromString(String personString) { return null; // No conversion fromString needed. } }); }
欢迎选择我的课程,让我们一起见证您的进步~~