How to display a tooltip according to mouse position? - JavaFX


I have a stackPane, filled with a Circle and a couple of lines.

I want to display a tooltip while hovering over the StackPane and the tooltip should contain the X/Y coords of the mouse.

I know how to get the Coords of the mouse, but I'm unable to find a way of showing the tool tip.

Can any of ou guys help me with that?..

1/16/2014 10:33:31 AM

Accepted Answer

try this...

Tooltip tp = new Tooltip("at stack tool");
stackpane.setOnMouseEntered(new EventHandler<MouseEvent>() {
     public void handle(MouseEvent t) {
          Node  node =(Node)t.getSource();
, FxApp.stage.getX()+t.getSceneX(), FxApp.stage.getY()+t.getSceneY());
1/16/2014 11:51:53 AM

Anshul Parashar's answer probably works, but ToolTip also has a 'installation' static helper method to handle display on hover.

Assuming n is a Node:

Tooltip tp = new Tooltip("at stack tool");
Tooltip.install(n, tp);

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow