如何在JavaFX中监听TableView列可见属性更改

时间:2016-10-19 14:13:13

标签: javafx binding javafx-8

我有一个启用了上下文菜单的表,允许用户隐藏/显示列。我想显示一个标签,指出哪些列丢失。我想用StringBinding来完成这个。我想知道是否有一种更简单的方法来监听将每个列可见属性添加为依赖项的更改?

    StringBinding hiddenColumnBinding = Bindings.createStringBinding(() -> {
        String text = "";
        Optional<String> columns = transactionTableView.getColumns()
                                        .stream()
                                       .filter(column -> !column.isVisible())
                                       .map(column -> column.getText())
                                       .reduce((first, second) -> first + ", " + second);
        if(columns.isPresent()){
            text = "Hidden Columns: " + columns.get();
        }
        return text;
    }, WHAT GOES HERE?);
    hiddenColumns.textProperty().bind(hiddenColumnBinding);

由于

1 个答案:

答案 0 :(得分:1)

我发现我可以使用-transactionTableView.getVisibleLeafColumns()

StringBinding hiddenColumnBinding = Bindings.createStringBinding(() -> {
    String text = "";
    Optional<String> columns = transactionTableView.getColumns()
                                    .stream()
                                   .filter(column -> !column.isVisible())
                                   .map(column -> column.getText())
                                   .reduce((first, second) -> first + ", " + second);
    if(columns.isPresent()){
        text = "Hidden Columns: " + columns.get();
    }
    return text;
}, transactionTableView.getVisibleLeafColumns());
    hiddenColumns.textProperty().bind(hiddenColumnBinding);