Set Tooltip for column header of treetableview in

2019-08-31 01:29发布

问题:

I have dynamic TreeTable. how can i set tooltip for column header, which shows the header text. The code bellow shows tooltip for specific cells, but i do not know where to set it for header text.

    for (Entry<String, String> ent : dc.getSortedAssignedOrg().entrySet()) {

        TreeTableColumn<String, ArrayList<String>> col = new TreeTableColumn<>(
                ent.getValue());
col.setCellFactory(new Callback<TreeTableColumn<String, ArrayList<String>>, TreeTableCell<String, ArrayList<String>>>() {
            @Override
            public TreeTableCell<String, ArrayList<String>> call(
                    TreeTableColumn<String, ArrayList<String>> param) {
                return new TreeTableCell<String, ArrayList<String>>() {
                    public void updateItem(ArrayList<String> item,
                            boolean empty) {
                        super.updateItem(item, empty);


                        if (item == null || empty) {
                            setStyle("");
                            setText("");
                        } else if (item.contains("Green")) {
                            float weightInt = Float.parseFloat(item.get(0));
                            float res = weightInt * 1;
                            String resString = Float.toString(res);
                            this.setStyle("-fx-background-color:green");
                            setTooltip(new Tooltip(item.get(2)));
                            setText(resString);
                        } else if (item.contains("yellow")) {
                            this.setStyle("-fx-background-color:yellow");
                            setTooltip(new Tooltip(item.get(2)));
                            setText("0");
                        } else if (item.contains("white")) {
                            setText("DD");
                        }
                    }
                };
            };

        });

        treeTableView.getColumns().add(col);

}

回答1:

Instead of setting text for the column, create a Label and set it as the graphic for the column. Then set the tooltip on the label.

I.e. instead of

    TreeTableColumn<String, ArrayList<String>> col = new TreeTableColumn<>(
            ent.getValue());

do

    TreeTableColumn<String, ArrayList<String>> col = new TreeTableColumn<>();
    Label label = new Label(ent.getValue());
    col.setGraphic(label);
    label.setTooltip(new Tooltip("Tooltip text goes here"));