How to display a tooltip according to mouse position? - JavaFX
13,572
Solution 1
try this...
Tooltip tp = new Tooltip("at stack tool");
stackpane.setOnMouseEntered(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
Node node =(Node)t.getSource();
tp.show(node, FxApp.stage.getX()+t.getSceneX(), FxApp.stage.getY()+t.getSceneY());
}
});
Solution 2
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);
Solution 3
I solved it like this:
Tooltip mousePositionToolTip = new Tooltip("");
gridPane.setOnMouseMoved(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
String msg = "(x: " + event.getX() + ", y: " + event.getY() + ")\n(sceneX: "
+ event.getSceneX() + ", sceneY: " + event.getSceneY() + ")\n(screenX: "
+ event.getScreenX() + ", screenY: " + event.getScreenY() + ")";
mousePositionToolTip.setText(msg);
Node node = (Node) event.getSource();
mousePositionToolTip.show(node, event.getScreenX() + 50, event.getScreenY());
}
});
It will show a ToolTip in right of your mouse-pointer. You can replace your StackPane with gridPane in my code and it should work. But i didn't test it.
![NexusTeddy](https://i.stack.imgur.com/EGact.jpg?s=256&g=1)
Author by
NexusTeddy
Updated on June 04, 2022Comments
-
NexusTeddy about 2 years
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?..
-
whossname almost 9 yearsToolTip.install(n, tp); should be Tooltip.install(n, tp);
-
Wolfgang Fahl almost 6 yearsmixing awt and javafx is not a good idea when you need positions in the javafx reference system
-
Ash over 5 yearsThank you @budo you way I found the best