1

There is a way to set tooltip like that

if (tooltipText != null) {
    Label lb = new Label(column.getText());
    lb.setTooltip(new Tooltip(tooltipText));
    column.setText(null);
    column.setGraphic(lb);
}

Unfortunately then will be exists ugly side-effect.

Empty names in table's menu

We set null to text of column but menuItems invoke column.getText(). If we don't do this, there will be double name in header.How to solve it? Suppose by means of css..

This answer doesn't erase side-effect How to add a tooltip to a TableView header cell in JavaFX 8

Community
  • 1
  • 1
DmitriyAntonov
  • 107
  • 1
  • 9

1 Answers1

2

Note: The following code relies on internals of the TableView skin. These could be subject to change, since they reside in the com.sun packages.

CSS lookup can be used to get access to the TableColumnHeader nodes after the first layout pass on the TableView. Those can be used to retrieve the Label that is used to display the TableColumn's text property.

@Override
public void start(Stage primaryStage) {
    TableView tv = createTableView();

    Scene scene = new Scene(tv);
    
    // generate layout pass
    tv.applyCss();
    tv.layout();

    // assign tooltips to headers
    tv.lookupAll(".column-header").stream().forEach(n -> {
        TableColumnHeader header = (TableColumnHeader) n;
        Tooltip tooltip = new Tooltip("Tooltip: " + header.getTableColumn().getText());
        ((Control) header.lookup(".label")).setTooltip(tooltip);
    });

    primaryStage.setScene(scene);
    primaryStage.show();
}
Abra
  • 19,142
  • 7
  • 29
  • 41
fabian
  • 80,457
  • 12
  • 86
  • 114