java 如何在 JavaFX 8 中隐藏 TableView 列标题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27118872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to hide TableView column header in JavaFX 8?
提问by user3804927
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:
我需要有一个可观察到的类型的一个类型,它将在一个单个列中显示在TableView中,选择后将在右侧显示其其余信息。TableView 包装在 TitledPane 中,后者包装在 Accordion 中。见下图:
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);
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。lookup("TableHeaderRow") 方法返回 null,这让我认为“TableHeaderRow”选择器不再存在。
Is there an updated workaround for removing/hiding the table header in JavaFX 8?
是否有更新的解决方法来删除/隐藏 JavaFX 8 中的表头?
回答by Christian Lutz
I faced the problem of hiding column headers recently and could solve it using css.
我最近遇到了隐藏列标题的问题,可以使用 css 解决它。
I created a styleclass:
我创建了一个样式类:
.noheader .column-header-background {
-fx-max-height: 0;
-fx-pref-height: 0;
-fx-min-height: 0;
}
and added it to the TableView:
并将其添加到 TableView:
tableView.getStyleClass().add("noheader");
Just in case someone needs an alternative approach. It also gives the flexibility of toggling column headers.
以防万一有人需要替代方法。它还提供了切换列标题的灵活性。
回答by James_D
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.
正如评论中所观察到的,在 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 mechanismthat is similar to that used for TableColumn
s (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
. 该ListView
有一个单元格呈现机制,即类似于用于TableColumn
S(但你不必对多个列的担心简单)。我会ListView
在你的场景中使用 a ,而不是破解 css 使标题消失:
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
}
);
回答by zIronManBox
Also, I would recommend using this nowadays.
另外,我建议现在使用它。
tableView.skinProperty().addListener((a, b, newSkin) -> {
TableHeaderRow headerRow = ((TableViewSkinBase)
newSkin).getTableHeaderRow();
...
});
This can be executed during initialization, the other method as mention above, will return null, if run during initialization.
这可以在初始化期间执行,如果在初始化期间运行,上面提到的另一种方法将返回 null。
回答by Don Wills
There's no need for CSS or style or skin manipulation. Simply make a subclass of TableView and override resize, like this
不需要 CSS 或样式或皮肤操作。只需创建一个 TableView 的子类并覆盖调整大小,就像这样
class XTableView extends TableView {
@Override
public void resize(double width, double height) {
super.resize(width, height);
Pane header = (Pane) lookup("TableHeaderRow");
header.setMinHeight(0);
header.setPrefHeight(0);
header.setMaxHeight(0);
header.setVisible(false);
}
}
This works fine as of June 2017 in Java 8.
截至 2017 年 6 月,这在 Java 8 中运行良好。