简体   繁体   English

当用户单击JavaFX中Node绑定的外部时失去焦点

[英]Lose focus when user clicks outside Node's bound in JavaFX

I am new to JavaFX and I need help... 我是JavaFX的新手,需要帮助...

I have a TextField and I want this Node lose focus when the user clicks outside its bounds. 我有一个TextField,我希望当用户在其边界之外单击时,此Node失去焦点。 Actually when the user clicks outside my TextField's bounds, the TextField keeps the focus. 实际上,当用户在TextField的边界之外单击时,TextField会保持焦点。

If someone has an idea on how to perform this action... thanks in advance! 如果有人对如何执行此操作有任何想法,请先谢谢! It will help me a lot. 这对我有很大帮助。

Thanks. 谢谢。

Hejk Hejk

Here's a quick implementation of the behavior advised using the scene 这是使用场景建议的行为的快速实现

scene.setOnMousePressed(event -> {
        if (!myFocusedNode.equals(event.getSource())) {
                   System.out.println("Clicked detected outside 'myFocusedNode' : {}", event.getSource());
                   myFocusedNode.getParent().requestFocus();
        }
});

It's a bit late response but I hope it will helps someone. 这是一个较晚的响应,但我希望它将对某人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM