JavaFX: how to set the correct tooltip position?

I have a TextField for which I want to show Tooltip in some circumstances.

After checking, I run the following code:

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

Therefore, when someone tries to log in with an empty username, he receives a Tooltip request by the "username" TextField .

But when it comes to action, Tooltip appears in the upper left corner of the screen.

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

+4
source share
1 answer

I think that the coordinates always refer to the screen. To calculate the coordinates of the components, you need to include the coordinates of the scene and window.

 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()); 
+10
source

Source: https://habr.com/ru/post/1441797/


All Articles