javafx: Custom TableColumn

陌路散爱 提交于 2020-01-25 21:32:31

问题


I want to make a custom TableColumn that when in editable state it's cells will be auto complete TextFields, here is what I tried:

public static <T,S> void setAutoCompleteTableColumn(TableColumn<T,S> column, List items){
    column.setCellFactory(param -> {
        return new TableCell<T, S>(){
            final TextField textField = new TextField();

            @Override
            protected void updateItem(S item, boolean empty) {
                super.updateItem(item, empty);

                if(item == null){
                    setGraphic(null);
                }else {

                    setGraphic(textField);
                    AutoCompletionBinding<T> binding = TextFields.bindAutoCompletion(textField,items);
                    binding.setOnAutoCompleted(event ->{
                        //handleCompleted.accept(event.getCompletion());
                    });
                }

            }
        };
    });
}

This code have produced (obviously) this:

But I want this form only when the TableView is in edit mode, So I did this instead:

public static <T,S> void setAutoCompleteTableColumn(TableColumn<T,S> column, List items){
    column.setCellFactory(param -> {
        return new TableCell<T, S>(){
            final TextField textField = new TextField();

            @Override
            protected void updateItem(S item, boolean empty) {
                super.updateItem(item, empty);

                if(item == null){
                    setGraphic(null);
                }else {
                    editableProperty().addListener((obs, oldValue, newValue)->{
                        if(newValue){
                            setGraphic(textField);
                        }else{
                            setText(item.toString());
                        }
                    });

                    AutoCompletionBinding<T> binding = TextFields.bindAutoCompletion(textField,items);
                    binding.setOnAutoCompleted(event ->{
                        //handleCompleted.accept(event.getCompletion());
                    });
                }

            }
        };
    });


}

As you can see I added a listener to editableProperty, which resulted in nothing. What I want is an editable TableColumn like in here, but with auto complete TextField. Any idea how to accomplish this?

来源:https://stackoverflow.com/questions/41678347/javafx-custom-tablecolumn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!