访问FXML控制器类 [英] Accessing FXML controller class

查看:89
本文介绍了访问FXML控制器类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想随时与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.

还要查看 https://stackoverflow.com/a/10718683/682495 .在JavaFX 2.2中,改进了FXMLLoader.

Also look the https://stackoverflow.com/a/10718683/682495. In JavaFX 2.2 FXMLLoader is improved.

这篇关于访问FXML控制器类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆