JavaFX: how to set correct tooltip position?

孤街醉人 提交于 2019-12-19 07:49:30

问题


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?


回答1:


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());


来源:https://stackoverflow.com/questions/13049362/javafx-how-to-set-correct-tooltip-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!