JavaFX TreeView tooltip conflicts with TreeCell tooltip

387 views Asked by At

when searching the web I couldn't find any hint how to solve the following problem: I have a JavaFX Pane containing a TreeView. In the very beginning this TreeView is empty, therefore I added a Tooltip in the fxml file, which suggests the user to use the right mouse button to get access to the context menu.

<TreeView fx:id="requirementsTreeView"  prefWidth="1000.0">
     <tooltip>
           <Tooltip text="Right mouse click to create root requirements" />
     </tooltip>
</TreeView>

This works fine. However, the same Tooltip was shown when the mouse was over an item in the TreeView - in other words over the TreeCell. Therefore, I added a different ToolTip to the TreeCell like this

private final class RequirementTreeCell extends TreeCell<UserRequirement> {

        @Override
        protected void updateItem(UserRequirement userRequirement, boolean empty) {
            super.updateItem(userRequirement, empty);
            if (!empty && userRequirement != null) {
                setText(userRequirement.toString());
                setGraphic(getTreeItem().getGraphic());
                setContextMenu(getContextMenu(userRequirement));
                setTooltip(new Tooltip("Right click to add sub-requirements"));
            } else {
                setText(null);
                setGraphic(null);
                setTooltip(null);
                setContextMenu(null);
            }
        }

..
}

but when the mouse is over a TreeCell still the ToolTip from the TreeView is shown. How can I make the Tooltip from the TreeView being displayed when the mouse is over an empty area of the TreeView and display the Tooltip of the TreeCell if the mouse is the over the TreeCell?

Thank you in advance!!

EDIT: I continued testing the functionality of the TreeView and added more TreeCells. As it turns out the behavior depends on the direction the mouse moves and on the TreeCells position. If the mouse comes from the Treeview area and moves over the last TreeCell, I get the wrong Tooltip. If I move the mouse further up, over the second last TreeCell I get the correct Tooltip after a while. If I then move back down over the last TreeCell I get the right ToolTip again. Sounds like a bug in Javafx?! What do you think?

1

There are 1 answers

1
James_D On

I guess you just need to set the tree's tooltip to null when it is not empty. In the controller you can do

Tooltip treeTooltip = new Tooltip("Right mouse click to create root requirements");
requirementsTreeView.tooltipProperty().bind(Bindings.createObjectBinding(() -> {
    if (requirementsTreeView.getRoot() == null) return treeTooltip ;
    return null ;
}, requirementsTreeView.rootProperty()));