有谁知道如何在TableView?
的列标题中添加工具提示有许多地方解释了如何将工具提示添加到数据单元格,但我没有找到将工具提示添加到标题的方法。
使用工具ScenicView,我可以看到标题是TableColumnHeader对象中的标签,但似乎它不是公共对象。
有什么建议吗?
答案 0 :(得分:18)
TableColumn<Person, String> firstNameCol = new TableColumn<>();
Label firstNameLabel = new Label("First Name");
firstNameLabel.setTooltip(new Tooltip("This column shows the first name"));
firstNameCol.setGraphic(firstNameLabel);
答案 1 :(得分:7)
这是对James_D的扩展答案。 (我没有评论的声誉):
要使标签与列的textProperty连接,只是隐藏原始文本,这样就不会弄乱其余的表功能:
nameLabel.textProperty().bindBidirectional(textProperty());
nameLabel.getStyleClass().add("column-header-label");
nameLabel.setMaxWidth(Double.MAX_VALUE); //Makes it take up the full width of the table column header and tooltip is shown more easily.
的CSS:
.table-view .column-header .label{
-fx-content-display: graphic-only;
}
.table-view .column-header .label .column-header-label{
-fx-content-display: text-only;
}
答案 2 :(得分:2)
或者,您可以查找已经存在的标签,并给它一个工具提示。
我尝试了Jesper和James的解决方案的组合。遗憾的是,当我在SceneBuilder中查看我的.fxml
布局时, CSS导致我的所有标签消失。我们就是不能拥有那个,是吗? (好吧也许我就是没有那个)。
为了.lookup()
元素,表格需要已经呈现。为了确保在呈现表之后运行代码,只需将代码包装在Platform.runlater()
。
//We need to do this after the table has been rendered (we can't look up elements until then)
Platform.runLater(() -> {
//Prepare a tooltip
Tooltip tooltip = new Tooltip("This is a super cool control; here's how to work it...");
tooltip.setWrapText(true);
tooltip.setMaxWidth(200);
//Get column's column header
TableColumnHeader header = (TableColumnHeader) historyTable.lookup("#" + column.getId());
//Get column header's (untooltipped) label
Label label = (Label) header.lookup(".label");
//Give the label a tooltip
label.setTooltip(tooltip);
// Makes the tooltip display, no matter where the mouse is inside the column header.
label.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
}
如果您想对整个列进行此操作,可以将它们全部放在LinkedHashMap中(这样可以通过保持列与消息相关联来帮助您进行组织管理):
//We'll use this to associate columns with messages for right now.
LinkedHashMap<TableColumn<Person, String>, String> tableColumns = new LinkedHashMap<>();
//Each column gets a helpful message
tableColumns.put(numberOfBoxesColumn, "The total number of boxes that have arrived");
tableColumns.put(backordersColumn, "Use these columns as a measure of how urgently the Purchase Order needs to be processed.");
/*... put each column in along with it's message */
...然后使用for-each循环遍历并为每列提供工具提示......
//Make a tooltip out of each message. Give each column('s label) it's tooltip.
for (Map.Entry<TableColumn<Person, String>, String> pair: tableColumns.entrySet()) {
TableColumn<Person, String> column;
String message;
//Get the column and message
column = pair.getKey();
message = pair.getValue();
//Prepare a tooltip
Tooltip tooltip = new Tooltip("This is a super cool control; here's how to work it...");
tooltip.setWrapText(true);
tooltip.setMaxWidth(200);
//Get column's column header
TableColumnHeader header = (TableColumnHeader) historyTable.lookup("#" + column.getId());
//Get column header's (untooltipped) label
Label label = (Label) header.lookup(".label");
//Give the label a tooltip
label.setTooltip(tooltip);
// Makes the tooltip display, no matter where the mouse is inside the column header.
label.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
}