【问题标题】:How to add a tooltip to a TableView header cell in JavaFX 8如何在 JavaFX 8 中向 TableView 标题单元格添加工具提示
【发布时间】:2014-06-07 03:17:00
【问题描述】:

有人知道如何在TableView 的列标题中添加工具提示吗?

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

使用ScenicView工具,可以看到表头是一个TableColumnHeader对象里面的Label,但是好像不是公共对象。

有什么建议吗?

【问题讨论】:

    标签: java user-interface javafx java-8 javafx-8


    【解决方案1】:
        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);
    

    【讨论】:

    • 有效!我需要删除 TableColumn 文本属性,但我得到了我需要的,感谢您的快速回答。
    • 请注意,如果您将 tableMenuButtonVisible 设置为 true 并使用此解决方案,则该按钮显示的菜单将不包含任何文本,因为它使用表格列的文本。
    • 感谢 James_D!我发现如果您将代码包装在 Platform.runLater(() -&gt; {} 中,您可以只使用 .lookup() 标头及其现有标签。感谢您帮助我解决所有问题!
    【解决方案2】:

    这是对 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;
    }
    

    【讨论】:

    • +1 表示setMaxWidth(),工具提示会更自然地为用户显示(悬停在标题的任何部分时,不一定是确切的文本部分本身)
    • 我发现label.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE); 是一种享受;工具提示出现在整个方块中,而不仅仅是与文本在同一行!
    • 样式类可以通过使用嵌套的标签选择器来简化(这样就不需要设置 id)。 e.i.:纯图形选择器.column-header Label 纯文本选择器.column-header Label Label
    【解决方案3】:

    解决方案

    或者,您可以查找已经存在的标签,然后给它一个工具提示。

    为了.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(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);
    }
    

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

    【讨论】:

      猜你喜欢
      • 2012-11-21
      • 1970-01-01
      • 2015-09-17
      • 1970-01-01
      • 1970-01-01
      • 2021-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多