如何在JavaFX 8中隐藏TableView列标题? [英] How to hide TableView column header in JavaFX 8?

查看:679
本文介绍了如何在JavaFX 8中隐藏TableView列标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有一个可观察的类型列表,该列表将显示在带有一个列的TableView中,选中后将在右侧显示其余信息。 TableView包含在TitledPane中,它包含在Accordion中。见下图:

I need to have an observable list of a type that will be displayed in a TableView with one single column, that when selected will display the rest of its information on the right. The TableView is wrapped in a TitledPane, which is wrapped in an Accordion. See image below:

正如您在此场景中所见,我不想显示列标题。

As you can see in this scenario I don't want to show the Column Header.

我试过以下此处的说明,指示此处

I tried following the instruction here, which leads to here:

Pane header = (Pane) list.lookup("TableHeaderRow");
header.setMaxHeight(0);
header.setMinHeight(0);
header.setPrefHeight(0);
header.setVisible(false);

然而,它似乎不适用于JavaFX 8.查找(TableHeaderRow)方法返回null让我觉得TableHeaderRow选择器不再存在。

However, it appears to not be working for JavaFX 8. The lookup("TableHeaderRow") method returns null which makes me think that the "TableHeaderRow" selector no longer exist.

是否有更新的解决方法来删除/隐藏JavaFX 8中的表头?

Is there an updated workaround for removing/hiding the table header in JavaFX 8?

推荐答案

正如在评论中所观察到的那样,在将CSS应用于节点之后,查找才会起作用,这通常是在第一帧渲染时显示节点。只要您在显示表格后执行已发布的代码,您建议的解决方案就可以正常工作。

As observed in the comments, lookups do not work until after CSS has been applied to a node, which is typically on the first frame rendering that displays the node. Your suggested solution works fine as long as you execute the code you have posted after the table has been displayed.

在这种情况下,为了更好的方法,单列表没有标题只是 ListView ListView 有一个单元格渲染机制类似于 TableColumn s(但更简单,因为您不必担心多列)。我会在你的场景中使用 ListView ,而不是黑客攻击css使标题消失:

For a better approach in this case, a single-column "table" without a header is just a ListView. The ListView has a cell rendering mechanism that is similar to that used for TableColumns (but is simpler as you don't have to worry about multiple columns). I would use a ListView in your scenario, instead of hacking the css to make the header disappear:

ListView<Album> albumList = new ListView<>();
albumList.setCellFactory((ListView<Album> lv) -> 
    new ListCell<Album>() {
        @Override
        public void updateItem(Album album, boolean empty) {
            super.updateItem(album, empty);
            if (empty) {
                setText(null);
            } else {
                // use whatever data you need from the album
                // object to get the correct displayed value:
                setText(album.getTitle());
            }
        }
    }
);

albumList.getSelectionModel().selectedItemProperty()
    .addListener((ObservableValue<? extends Album> obs, Album oldAlbum, Album selectedAlbum) -> {
        if (selectedAlbum != null) {
            // do something with selectedAlbum
        }
);

这篇关于如何在JavaFX 8中隐藏TableView列标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆