java JavaFX 2.0 + FXML。从不同的任务更新场景值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10107829/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:39:34  来源:igfitidea点击:

JavaFX 2.0 + FXML. Updating scene values from a different Task

javajavafx-2fxml

提问by Javier

I want to get the controller from a scene that i've loaded with FXMLoader. The use case is:

我想从我用 FXMLoader 加载的场景中获取控制器。用例是:

  1. My JSON manager receives a JSON object
  2. The task I've launched shows a new Scene using

    Parent p = FXMLLoader.load(getClass().getResource("foo.fxml"));
    Scene scene = new Scene(p);
    stage.setScene(scene);
    

    After that, i have the empty scene.

  3. Now I do this to fill the components

    AnchorPane pane = (AnchorPane)((AnchorPane) scene.getRoot()).getChildren().get(0);
    for(Node node : pane.getChildren()){
        String id = node.getId();
        if(id.equals(NAME)){
             ((TextField)node).setText(value);
        }
    }
    
  1. 我的 JSON 管理器收到一个 JSON 对象
  2. 我启动的任务显示了一个新场景,使用

    Parent p = FXMLLoader.load(getClass().getResource("foo.fxml"));
    Scene scene = new Scene(p);
    stage.setScene(scene);
    

    在那之后,我有空的场景。

  3. 现在我这样做是为了填充组件

    AnchorPane pane = (AnchorPane)((AnchorPane) scene.getRoot()).getChildren().get(0);
    for(Node node : pane.getChildren()){
        String id = node.getId();
        if(id.equals(NAME)){
             ((TextField)node).setText(value);
        }
    }
    

My question, is there an easier way to do this? I have a controller specified in FXML

我的问题是,有没有更简单的方法来做到这一点?我有一个在 FXML 中指定的控制器

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="526.0" minWidth="356.0" prefHeight="526.0" prefWidth="356.0" 
xmlns:fx="http://javafx.com/fxml" fx:controller="bar.foo">

I want to get the instance with the bind values (TextField called name in this case)

我想获取具有绑定值的实例(在本例中名为 name 的 TextField)

Thanks in advance

提前致谢

回答by Uluk Biy

1) You can get the controller from the FXMLLoaderbut don't know is it possible from Scene:

1)您可以从以下位置获取控制器,FXMLLoader但不知道是否可以从Scene

FXMLLoader fxmlLoader = new FXMLLoader();
Pane p = fxmlLoader.load(getClass().getResource("foo.fxml").openStream());
bar.foo fooController = (bar.foo) fxmlLoader.getController();

To use the fooControllerlater in a different part of your code, you can use Node#setUserData(). For example after the code above:

fooController在代码的不同部分使用后者,您可以使用Node#setUserData(). 例如在上面的代码之后:

p.setUserData(fooController);
...
// after a while of app logic and code
bar.foo controller_of_p = (bar.foo) p.getUserData();

This gives a workaround and a shortcut to achieve your goal.

这提供了解决方法和实现目标的捷径。

2) If your node has an id then you can directly Node#lookup()it rather than constructing a for-loop :

2)如果你的节点有一个id,那么你可以直接使用Node#lookup()它而不是构建一个for循环:

TextField txt = (TextField) pane.lookup("#nodeId");