Vasting
Vasting

Reputation: 289

How do I know which node is focused in JavaFX?

I have a lot of TextArea nodes in a Scene.

Is it possible for me to figure out which TextArea is selected (has the caret in it)?
I would like to be able to select the node and set it to a Node variable.

Upvotes: 11

Views: 8534

Answers (1)

jns
jns

Reputation: 6952

Actually there is no need to set a focused node variable, because Scene already contains a focusOwnerProperty.

So you could use it e.g. like:

    if (scene.focusOwnerProperty().get() instanceof TextArea) {
        TextArea focusedTextArea = (TextArea) scene.focusOwnerProperty().get();
    }

Upvotes: 23

Related Questions