Maison > Questions et réponses > le corps du texte
javafx 中combobox设置成combobox.setEditable(true);combobox就无法选择,也获取不到选择了的值。请问有遇到这种情况的嘛?
下面是代码
private ComboBox<StringProperty> peopleComBox;
private ObservableList<StringProperty> 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<StringProperty>() {
@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<StringProperty>() {
@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.
}
});
}