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

后端 未结 1 650
广开言路
广开言路 2020-12-19 16:31

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

  1. My JSON manager receives a JSON object
  2. The task I\'ve

相关标签:
1条回答
  • 2020-12-19 16:58

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

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

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

    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 :

    TextField txt = (TextField) pane.lookup("#nodeId");
    
    0 讨论(0)
提交回复
热议问题