Set Tooltip for column header of treetableview in Javafx

北战南征 提交于 2019-12-12 06:17:59

问题


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"));


来源:https://stackoverflow.com/questions/28878891/set-tooltip-for-column-header-of-treetableview-in-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!