过滤后的ComboBox中的文本编辑器不显示文本

时间:2019-05-25 20:34:35

标签: java javafx

我将Jonathan Stenbackas Answer(JavaFX - Filtered ComboBox)修改为带有Jooq记录Bean的ComboBox,并使用自定义ListCell / ButtonCell显示Bean字段之一。 筛选工作正常,但是我无法让编辑器显示所选项目字段的文本。

我在组合框的选择中添加了一个侦听器,并让它打印出编辑器文本。所以我得到了bean的toString(),但是Editor为空。当我使用侦听器以编程方式设置文本时,将以编程方式设置的文本打印出来,但编辑器不会显示它。 我还尝试了JavaFX bean进行测试,结果相同。 任何建议将不胜感激。

//retrieving data from database
ObservableList<MyRecord> items = applicationContext.getFetchData().fetchOList();

//wrapping in a FilteredList

FilteredList<MyRecord> filteredItems = new FilteredList<>(items, p -> true);

    ComboBox<MyRecord> cb = new ComboBox<>(filteredItems);
    cb.setEditable(true);
    cb.setCellFactory(c_ -> new NamenCell());
    cb.setButtonCell(new NamenCell());
    TextField editor = cb.getEditor();

    editor.textProperty().addListener((obs, oldValue, newValue) -> {

        final MyRecord selected = cb.getSelectionModel().getSelectedItem();

        Platform.runLater(() -> {
            if (selected == null || !selected.getSurname().equals(editor.getText())) {
                filteredItems.setPredicate(item -> {
                    if (item.getSurname().toLowerCase().contains(newValue.toLowerCase())) {
                        return true;
                    } else {
                        return false;
                    }
                });
            }

        });
    });


    cb.getSelectionModel().selectedItemProperty().addListener(
            (ob, oldValue, newValue) -> {
                if (newValue != null) {
                    cb.getEditor().setText(newValue.getSurname());
                    System.out.println(cb.getEditor().getText());

                }

            });


//The Cell class:

public class NamenCell extends ListCell<MyRecord> {

    public NamenCell() { }
    @Override
    protected void updateItem(MyRecord item, boolean empty) {
        super.updateItem(item, empty);
        setText(item == null ? "" : item.getSurname());

    }
}

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案:我扩展了组合框。 也许这对某人可能有用:



       import javafx.application.Platform;
    import javafx.beans.property.Property;
    import javafx.beans.property.SimpleObjectProperty;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.collections.transformation.FilteredList;
    import javafx.scene.control.ComboBox;
    import javafx.scene.control.TextField;
    import org.jooq.impl.TableRecordImpl;


    public class FilterComboBox extends ComboBox {
        private ObservableList ol;
        private ObservableList items;
        private FilteredList filteredItems;
        private FilteredList filteredOl;
        private String field;
        private Property selectedBeanProperty = new SimpleObjectProperty();

        public FilterComboBox(){

        }

        public FilterComboBox(ObservableList ol, String field) {
          populate(ol,field);
        }


        public Property selectedBeanProperty() {
            return selectedBeanProperty;
        }

        public void setSelectedBeanProperty(T selectedBeanProperty) {
            this.selectedBeanProperty.setValue(selectedBeanProperty);
        }

        private ObservableList createItems(ObservableList ol, String field) {
            this.items = FXCollections.observableArrayList();
            for (T t : ol) items.add(t.get(field).toString());
            return items;
        }

        public void populate(ObservableList ol, String field){
            this.field=field;
            this.ol = ol;
            this.items = createItems(this.ol, field);
            this.filteredItems = new FilteredList(items, p -> true);
            this.filteredOl= new FilteredList(ol, p->true);
            this.setEditable(true);
            this.setItems(filteredItems);

            TextField editor = this.getEditor();
            editor.textProperty().addListener((obs, oldValue, newValue) -> {
                final String selected = this.getSelectionModel().getSelectedItem();

                Platform.runLater(() -> {
                    if (selected == null || !selected.equals(editor.getText())) {
                        filteredItems.setPredicate(item -> {
                            if (item.toLowerCase().contains(newValue.toLowerCase())) {
                                return true;
                            } else {
                                return false;
                            }
                        });
                    }


                });

            });

            this.getSelectionModel().selectedItemProperty().addListener((obs, oldValue, newValue) -> {
                if (newValue != null &&!newValue.isEmpty() ) {
                    setSelectedBeanProperty(this.ol.get(getOlIndex(newValue, field)));
                }
            });
        }

        public Integer getOlIndex(String s, String field){
            for (T t : ol)
                if(t.get(field).toString().equals(s)){
                return ol.indexOf(t);
            }
            return -1;
        }

    }