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");
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…