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

Rob*_*rto 10 java user-interface javafx java-8 javafx-8

有谁知道如何将工具提示添加到TableView中的列标题?

有很多地方解释了如何将工具提示添加到数据单元格,但我没有找到将工具提示添加到标题的方法.

使用ScenicView工具,我可以看到标题是TableColumnHeader对象中的标签,但似乎它不是公共对象.

有什么建议 ?

Jam*_*s_D 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);
Run Code Online (Sandbox Code Playgroud)

  • 请注意,如果将`tableMenuButtonVisible`设置为true并使用此解决方案,则该按钮显示的菜单将不包含任何文本,因为它使用表列的文本. (4认同)

小智 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.
Run Code Online (Sandbox Code Playgroud)

CSS:

.table-view .column-header .label{
    -fx-content-display: graphic-only;
}
.table-view .column-header .label .column-header-label{
    -fx-content-display: text-only;
}
Run Code Online (Sandbox Code Playgroud)


Bra*_*rek 5

解决方案

或者,您可以查找已有的标签并为其提供工具提示。

为了成为.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);
}
Run Code Online (Sandbox Code Playgroud)

大规模解决方案

如果您想对整个列集执行此操作,您可以将它们全部放入 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 */
Run Code Online (Sandbox Code Playgroud)

...然后使用 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);
}
Run Code Online (Sandbox Code Playgroud)

注意:在提出这个解决方案之前,我尝试了 Jesper 和 James 的解决方案的组合。遗憾的是,当我这样做时,当我在 SceneBuilder 中查看布局时,CSS 导致所有标签消失.fxml。我们就是不能这样,不是吗?(好吧,也许我就是不能拥有那个)。

麻标签在哪里!