Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX: how to set correct tooltip position?

I have a TextField for which I want a Tooltip to be shown under some circumstances.

After performing checks I run the followig code:

textFieldUsername.setTooltip(new Tooltip("Enter username!"));
textFieldUsername.getTooltip().setAutoHide(true);
textFieldUsername.getTooltip().show(textFieldUsername, 1, 1);

So when somebody tries to login with empty username he gets a prompting Tooltip over the "username" TextField.

But when it comes to action, the Tooltip pops up in the top left corner of the screen.

Should I calculate coords of my scene, then add my TextField coords to them, or there is a way to set these 1, 1 arguments from the call of show() to be relative to the TextField position?

like image 513
Chechulin Avatar asked Oct 24 '12 12:10

Chechulin


1 Answers

I think the coordinates are always relative to the screen. To calculate component coordinates you need to incorporate scene and window coordinates.

Point2D p = label.localToScene(0.0, 0.0);
label.getTooltip().show(label,
        p.getX() + label.getScene().getX() + label.getScene().getWindow().getX(),
        p.getY() + label.getScene().getY() + label.getScene().getWindow().getY());
like image 200
amru Avatar answered Oct 05 '22 00:10

amru