访问FXML控制器类

编程入门 行业动态 更新时间:2024-10-27 08:35:12
本文介绍了访问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.

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

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

更多推荐

访问FXML控制器类

本文发布于:2023-11-17 04:22:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608813.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   FXML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!