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

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

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

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

有什么建议么 ?

TableColumn firstNameCol = new TableColumn<>(); Label firstNameLabel = new Label("First Name"); firstNameLabel.setTooltip(new Tooltip("This column shows the first name")); firstNameCol.setGraphic(firstNameLabel); 

这是对James_D的扩展答案。 (我没有评论的声誉):

要使标签与列的textProperty连接并只隐藏原始文本,因此它不会弄乱表的其余function:

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

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

我尝试了Jesper和James的解决方案的组合。 可悲的是,当我在.fxml中查看我的.fxml布局时, CSS导致我的所有标签都消失了 。 我们就是不能拥有那个,是吗? (好吧, 也许我不能那样)。

哪里是mah标签!

为了.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, 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, String> pair: tableColumns.entrySet()) { TableColumn 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); }