java TextField 中的 RequestFocus 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12744542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RequestFocus in TextField doesn't work
提问by Adil
I use JavaFX 2.1 and I created GUI using FXML, in the controller of this GUI I added myTextField.requestFocus();
.
我使用 JavaFX 2.1 并使用 FXML 创建了 GUI,在我添加的这个 GUI 的控制器中myTextField.requestFocus();
。
But I always get the focus in the other control.
但我总是把焦点放在另一个控件上。
回答by Sergey Grinev
At the time of initialize()
controls are not yet ready to handle focus.
在initialize()
控件还没有准备好处理焦点的时候。
You can try next trick:
您可以尝试下一个技巧:
@Override
public void initialize(URL url, ResourceBundle rb) {
Platform.runLater(new Runnable() {
@Override
public void run() {
tf.requestFocus();
}
});
}
For tricky complex applications (like Pavel_K has in the comments) you may want to repeat this routine several times and call method line next one:
对于棘手的复杂应用程序(如 Pavel_K 在注释中的应用),您可能需要多次重复此例程并调用下一行的方法:
private void repeatFocus(Node node) {
Platform.runLater(() -> {
if (!node.isFocused()) {
node.requestFocus();
repeatFocus(node);
}
});
}
Note this is undocumented approach and it may be wise to add a limit for repetitions to avoid endless loop if something changed or broke in future Java releases. Better to lose focus than a whole app. :)
请注意,这是未记录的方法,如果在未来的 Java 版本中发生更改或损坏,则添加重复限制以避免无限循环可能是明智的。比整个应用程序更好地失去焦点。:)
回答by Sedrick
The exact same answer as @Sergey Grinev. Make sure your version of java is up-to-date (JDK 1.8 or later).
与@Sergey Grinev 完全相同的答案。确保您的 java 版本是最新的(JDK 1.8 或更高版本)。
Platform.runLater(()->myTextField.requestFocus());
回答by codepleb
If you requestFocus(); AFTER initializing the scene, it will work!
如果你 requestFocus(); 初始化场景后,它将起作用!
Like this:
像这样:
Stage stage = new Stage();
GridPane grid = new GridPane();
//... add buttons&stuff to pane
Scene scene = new Scene(grid, 800, 600);
TEXTFIELD.requestFocus();
stage.setScene(scene);
stage.show();
I hope this helps. :)
我希望这有帮助。:)
回答by nickthecoder
This can occur when the Scene property for the Node is not yet set. Alas, the scene property can take a "long" time to be set.
当节点的 Scene 属性尚未设置时,可能会发生这种情况。唉,场景属性可能需要“很长时间”才能设置。
The child node's scene property lags when a scene is first created, and also, when items are added to some parents, such as a TabPane (oddly some parents seem immune, I'm not sure why).
子节点的场景属性在首次创建场景时滞后,并且当项目添加到某些父节点时,例如 TabPane(奇怪的是有些父节点似乎免疫,我不知道为什么)。
The correct strategy, which has always worked for me :
正确的策略,它一直对我有用:
if (myNode.scene) == null {
// listen for the changes to the node's scene property,
// and request focus when it is set
} else {
myNode.requestFocus()
}
I have a handy Kotlin extension function which does this.
我有一个方便的 Kotlin 扩展函数可以做到这一点。
fun Node.requestFocusOnSceneAvailable() {
if (scene == null) {
val listener = object : ChangeListener<Scene> {
override fun changed(observable: ObservableValue<out Scene>?, oldValue: Scene?, newValue: Scene?) {
if (newValue != null) {
sceneProperty().removeListener(this)
requestFocus()
}
}
}
sceneProperty().addListener(listener)
} else {
requestFocus()
}
}
You can then call it from within you code like so :
然后,您可以从代码中调用它,如下所示:
myNode.requestFocusOnSceneAvailable()
Perhaps somebody would like to translate it to Java.
也许有人想把它翻译成Java。