简体   繁体   English

JavaFX TableColumn图形不隐藏

[英]JavaFX TableColumn Graphic not Hiding

I'm creating a custom header for my TableColumns that is the label of the column plus a TextField that will allow users to perform searches. 我正在为我的TableColumns创建一个自定义标题,该标题是列的标签以及允许用户执行搜索的TextField。 I'm setting the column headers like so: 我正在设置列标题,如下所示:

getColumns().addListener(new ListChangeListener<TableColumn<S, ?>>() {
        @Override
        public void onChanged(final ListChangeListener.Change<? extends TableColumn<S, ?>> change) {
            while (change.next()) {
                Label label;
                TextField search;
                VBox graphic;
                for (TableColumn<S, ?> column : change.getAddedSubList()) {
                    label = new Label(column.getText());
                    search = new TextField();
                    graphic = new VBox();
                    graphic.getStyleClass().add("k-column-graphic");
                    graphic.getChildren().addAll(label, search);
                    column.setGraphic(graphic);
                }
            }
        }
    });

So the column's graphic is what is displayed. 因此列的图形是显示的内容。 I'm using the following CSS (the graphic itself has a "k-column-graphic" CSS class, while the TableView has a "k-table-view" CSS class) 我正在使用以下CSS(图形本身有一个“k-column-graphic”CSS类,而TableView有一个“k-table-view”CSS类)

/** Hide default text label in KTableView */
.k-table-view .column-header > .label  {
    -fx-content-display: graphic-only;
}

.k-column-graphic {
    -fx-alignment: center-left;
    -fx-spacing: 5;
    -fx-padding: 2;
}

This works great, but I'm also allowing the columns to be hidden by enabling the TableView.setTableMenuButtonVisible(true); 这很好用,但我也允许通过启用TableView.setTableMenuButtonVisible(true);隐藏列TableView.setTableMenuButtonVisible(true); property, which adds a button to easily hide columns. 属性,添加一个按钮以轻松隐藏列。

Whenever I try to hide a column, it hides successfully, but the graphic (the Label/TextField) remain. 每当我尝试隐藏列时,它都会成功隐藏,但图形(Label / TextField)仍然存在。 Both seem to have a width of 0 or 1, and are very small, but you can still see them. 两者似乎都有0或1的宽度,并且非常小,但您仍然可以看到它们。

全部可见

网站隐藏

How, either through CSS or somewhere in my code, do I make it to where the graphic Node for the TableColumn will hide as well? 如何通过CSS或我的代码中的某个地方,将它用于TableColumn的图形节点也将隐藏的位置?

When you toggle the CheckMenuItem to show/hide the column, your customized controls won't automatically change their values of VisibleProperty . 当您切换CheckMenuItem以显示/隐藏列时,您的自定义控件将不会自动更改其VisibleProperty的值。 So what you need to do is simply bind the VisibleProperty of your own controls to the TableColumn 's VisibleProperty . 所以你需要做的只是将你自己的控件的VisibleProperty绑定到TableColumnVisibleProperty

Following sample is based on your code. 以下示例基于您的代码。 Hoping it can help. 希望它可以帮助。

    getColumns().addListener(new ListChangeListener<TableColumn<S, ?>>() {
        @Override
        public void onChanged(final ListChangeListener.Change<? extends TableColumn<S, ?>> change) {
            while (change.next()) {
                Label label;
                TextField search;
                VBox graphic;
                for (TableColumn<S, ?> column : change.getAddedSubList()) {
                    label = new Label(column.getText());
                    search = new TextField();
                    graphic = new VBox();
                    graphic.getStyleClass().add("k-column-graphic");
                    graphic.getChildren().addAll(label, search);
                    column.setGraphic(graphic);

                    /* ======= add the following two lines ============== */
                    label.visibleProperty().bind(column.visibleProperty());
                    search.visibleProperty().bind(column.visibleProperty());
                }
            }
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM