How to add a tooltip to a TableView header cell in JavaFX 8

后端 未结 3 1829
时光说笑
时光说笑 2021-02-13 20:34

Does anyone know how to add a tooltip to a column header in a TableView ?

There are many places where are explained how to add the tooltip to data cells, but I didn\'t f

3条回答
  •  北海茫月
    2021-02-13 21:30

    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. To be sure that your code runs after the table is rendered, just wrap your code in a 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):

    // 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 */
    

    ... then use a for-each loop to loop through and give each column a tooltip ...

    // 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(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. Sadly, when I did, the CSS caused all of my labels to disappear when I look at my .fxml layout in SceneBuilder. And we just can't have that, can we? (okay maybe I just couldn't have that).

提交回复
热议问题