简体   繁体   English

如何在 JavaFX 8 中向 TableView 标题单元格添加工具提示

[英]How to add a tooltip to a TableView header cell in JavaFX 8

Does anyone know how to add a tooltip to a column header in a TableView ?有谁知道如何向TableView 中的列标题添加工具提示?

There are many places where are explained how to add the tooltip to data cells, but I didn't find a way to add the tooltip to header.有很多地方解释了如何将工具提示添加到数据单元格,但我没有找到将工具提示添加到标题的方法。

Using the tool ScenicView , I can see that the headers are Labels inside a TableColumnHeader object, but It seems that It is not a public object.使用工具ScenicView ,我可以看到标题是 TableColumnHeader 对象内的标签,但它似乎不是公共对象。

Any suggestions ?有什么建议 ?

    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);

This is an extended answer to James_D.这是对 James_D 的扩展答案。 (I don't have the reputation to comment): (我没有评论的声誉):

To make the label connect with textProperty of the column and just hide the original text, so it does not mess up the rest of the table functionality:为了使标签与列的 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: css:

.table-view .column-header .label{
    -fx-content-display: graphic-only;
}
.table-view .column-header .label .column-header-label{
    -fx-content-display: text-only;
}

The solution解决方案

Alternatively, you can look up the label that's already there and just give it a tool tip.或者,您可以查找已经存在的标签并为其提供工具提示。

In order to .lookup() an element the table needs to be rendered already.为了.lookup()一个元素,表格需要已经呈现。 To be sure that your code runs after the table is rendered, just wrap your code in a Platform.runlater() .为了确保您的代码在表格呈现后运行,只需将您的代码包装在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);
}

The solution at scale大规模解决方案

If you want to do this for your whole set of columns, you might put them all in a LinkedHashMap (this will just help you organizationally by keeping your columns associated with their messages):如果您想对整个列执行此操作,您可以将它们全部放在 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 */

... then use a for-each loop to loop through and give each column a tooltip ... ...然后使用 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(message);
    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);
}

NOTE: I tried the combination of Jesper and James' solutions before coming up with this one.注意:在提出这个解决方案之前,我尝试了 Jesper 和 James 的解决方案的组合。 Sadly, when I did, the CSS caused all of my labels to disappear when I look at my .fxml layout in SceneBuilder.可悲的是,当我这样做时,当我在.fxml中查看我的.fxml布局时, CSS 导致我的所有标签消失 And we just can't have that, can we?我们不能拥有那个,对吗? (okay maybe I just couldn't have that). (好吧,也许我就是做不到)。

哪里有麻标签!

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

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