javafx combobox在edit状态下无法选择
天蓬老师
天蓬老师 2017-04-18 10:50:03
0
0
841

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.
            }
        });

        
        

}
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

Antworte allen(0)
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage