By
n.setFocusTraversable(false);
node runs without focus, and not without focus. It can still be focused, for example, with the mouse or programmatically. Since you prevented mouse events, here is another option:
Platform.runLater(new Runnable() { @Override public void run() { textfield.requestFocus(); } }); Scene scene = new Scene(root);
EDIT: according to the comment,
The javadoc requestFocus says:
... To be eligible for focus, the node must be part of the scene, it and all its ancestors must be visible, and this should not be turned off ....
So, this method should be called after plotting the scene as follows:
Scene scene = new Scene(root); textfield.requestFocus();
However, Platform.runLater in the example above will be launched at the end after the main start() method, which ensures that the requestFocus call will be executed after plotting the scene.
There may be other reasons depending on the requestFocus implementation code.
Uluk Biy
source share