Hejk
Hejk

Reputation: 195

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

I am new to JavaFX and I need help...

I have a TextField and I want this Node lose focus when the user clicks outside its bounds. Actually when the user clicks outside my TextField's bounds, the TextField keeps the focus.

If someone has an idea on how to perform this action... thanks in advance! It will help me a lot.

Thanks.

Hejk

Upvotes: 3

Views: 1956

Answers (1)

Mr Pilli
Mr Pilli

Reputation: 21

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.

Upvotes: 2

Related Questions