我想随时与FXML控制器类进行通信,以从主应用程序或其他阶段更新屏幕上的信息.
I would like to communicate with a FXML controller class at any time, to update information on the screen from the main application or other stages.
这可能吗?我还没有找到任何方法.
Is this possible? I havent found any way to do it.
静态函数可能是一种方法,但是它们无法访问表单的控件.
Static functions could be a way, but they don't have access to the form's controls.
有什么想法吗?
推荐答案您可以从FXMLLoader
FXMLLoader fxmlLoader = new FXMLLoader(); Pane p = fxmlLoader.load(getClass().getResource("foo.fxml").openStream()); FooController fooController = (FooController) fxmlLoader.getController();将其存储在您的主阶段,并提供getFooController()getter方法. 在其他类或阶段中,每次需要刷新加载的"foo.fxml"页面时,请从其控制器中进行询问:
store it in your main stage and provide getFooController() getter method. From other classes or stages, every time when you need to refresh the loaded "foo.fxml" page, ask it from its controller:
getFooController().updatePage(strData);updatePage()可以像这样:
updatePage() can be something like:
// ... @FXML private Label lblData; // ... public void updatePage(String data){ lblData.setText(data); } // ...在FooController类中. 这样,其他页面用户就不必担心页面的内部结构,例如Label lblData的位置和位置.
in the FooController class. This way other page users do not bother about page's internal structure like what and where Label lblData is.
还要查看 stackoverflow/a/10718683/682495 .在JavaFX 2.2中,改进了FXMLLoader.
Also look the stackoverflow/a/10718683/682495. In JavaFX 2.2 FXMLLoader is improved.
更多推荐
访问FXML控制器类
发布评论