如何引用primaryStage [英] How to reference primaryStage

查看:758
本文介绍了如何引用primaryStage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将.fxml-Files用于我的应用程序的视图层。每个fxml都有一个连接到它的控制器

I use .fxml-Files for the view-layer of my application. Each fxml has a controller attached to it

<AnchorPane fx:controller="movielistjavafx.view.MainWindowController">

假设我有一个mainFrame和它的控制器。 mainFrame.fxml加载在 start(舞台) -method。

Let's assume I have a mainFrame and it's controller. The mainFrame.fxml is loaded in the start(Stage)-method.

现在你要显示一个fileChooser附加到Stage / Window / Whatever。

Now you would like to show a fileChooser which is attached to a Stage/Window/Whatever.

为此,让fxml-controller了解例如 primaryStage

For that it would be good to let the fxml-controller know about the for example primaryStage.

有没有办法将它注入控制器,或者FXML是否在运行时知道它属于哪个场景和阶段?

Is there any way to inject it to the controller, or does the FXML know at runtime to which scene and stage it belongs?

我只有想法是将primaryStage存储在某个静态上下文中,但这似乎不是对我这样做的方法。

Only idea I have is to store primaryStage in some static context, but that seems not like a way to do it to me.

推荐答案

不是FXML,但FXML(或其控制器)中的节点(控件)知道它们在运行时属于哪个场景和阶段(添加到场景后。)
在控制器类中,

Not FXML but the nodes (controls) in FXML (or in its Controller) know to which scene and stage they belong at runtime (after being added to the scene).
In controller class,

...
@FXML private Label label;
...
// in some method block
Stage stageTheLabelBelongs = (Stage) label.getScene().getWindow();

或者,您可以使用CDI事件来获得主要阶段。查看博客文章 FXML& ; JavaFX由CDI& JBoss Weld

Alternatively you can use CDI events to get the primary stage. Look the blog entry FXML & JavaFX powered by CDI & JBoss Weld.

这篇关于如何引用primaryStage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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