15

It is possible to show a Tooltip on a disabled Control?

I have the following code and this doesn't work:

txt_searchText.setDisable(true);
txt.searchText.setTooltip(new Tooltip("Message"));

Has anyone a solution for that problem?

Thx

Flagman
  • 416
  • 6
  • 24
  • 1
    no solution, just an observation: looks like mouseEvents aren't delivered to disabled controls (nor nodes, probably) – kleopatra Jun 10 '14 at 14:39
  • FYI, asked my own question about (what I think is the) underlying reason for the behaviour http://stackoverflow.com/q/24182802/203657 – kleopatra Jun 12 '14 at 11:08

4 Answers4

22

The answer is no. Currently you cannot show a tooltip on disabled Node, for the simple reason that disabled Nodes do not receive any MouseEvents.

You can see the issue being raised in the official issue tracler here (require login) : https://javafx-jira.kenai.com/browse/RT-28850

One solution to your problem could be to wrap your Control into something else.

For example, put your control into another Control, like a SplitPane or a Label. Then you could apply your tooltip to that wrapper and disable your first control.

Synchro
  • 35,538
  • 15
  • 81
  • 104
Maxoudela
  • 2,091
  • 3
  • 15
  • 23
  • I moved the tooltip to a Label wrapper but found when the Node inside was enabled it would prevent the Label's tooltip from showing. To make it work whether the Node was enabled or disabled, I put the tooltip on both the Node and the wrapper Label. – Zyxl Jun 05 '23 at 13:39
2

Not directly but you can warp your button into another control and while your button could be disable or not, the control will answer to mouse movements.

Button button = new Button("Click me");     //create a button
button.setDisable(true);        //disable button in some way
SplitPane splitPane = new SplitPane(button);   //warp it into a splitPane
splitPane.setTooltip(new Tooltip("I'm the Tooltip Massage")); //Crete a tooltip

Node that SplitPane extends "Controls" not Region and not pane.

so it's a Control and best for our case (warping buttons).

you must always use a Control to warp another control. other way you will not have access to setTooltip() method.

ARiyou Jahan
  • 622
  • 6
  • 7
0

Here's a workaround using the CustomMenuItem class:

    customMenuItem.getContent().setOnMouseEntered(e -> {
        if (customMenuItem.isDisable()) {
            Tooltip.install(customMenuItem.getContent(), tooltip);
        } else {
            Tooltip.uninstall(customMenuItem.getContent(), tooltip);
        }
    });
Displee
  • 670
  • 8
  • 20
0

Another solution is to filter mouse events on the parent and display a tooltip on the disabled items. A typical example is a toolbar:

toolBar.addEventFilter(MouseEvent.MOUSE_MOVED, e -> {
    
    var node = toolBar.getItems().stream()
            .filter(Node::isDisabled)
            .filter(n -> n.contains(n.parentToLocal(e.getX(), e.getY()))).findFirst();
    
    if (node.isPresent() && node.get() instanceof Control control) {
        toolBar.setTooltip(control.getTooltip());
    } else {
        toolBar.setTooltip(null);
    }
});
appsofteng
  • 152
  • 5