问题描述
要显示以下代码的模态对话框:
To show modal dialog following code should be used:
val dialogStage:Stage = new Stage();
dialogStage.initOwner(ownerStageWindow)
dialogStage.initModality(javafx.stage.Modality.WINDOW_MODAL);
val scene = new Scene(...)
dialogStage.setScene(scene)
dialogStage.show();
但是在哪里以及如何获得所有者阶段?
没有初级阶段如果我使用JFXPanel作为根组件来嵌入JavaFX应用程序。
But where and how to get owner stage?
There is no primary stage if I use JFXPanel as root component to embed JavaFX application.
推荐答案
获取JFXPanel的阶段:
To get the stage of a JFXPanel:
Field f = JFXPanel.class.getDeclaredField("stage");
f.setAccessible(true);
stage = (Window)f.get(fxPanel);
但重要的是要注意,你获得的不一定是Stage的一个实例。它将是Window(Stage的超类)的一个实例。在swing应用程序的情况下,它将返回一个EmbeddedWindow实例(不是舞台)。
But it is important to note that what you get will not be necessarily be an instance of Stage. It will be an instance of Window (superclass of Stage). In the case of a swing application, it will return an EmbeddedWindow instance (which is not a Stage).
或者说明这个技术答案:否则你将只有一个如果您有纯JavaFx应用程序,请暂存。如果我错了,请纠正我。
Or to state this technical answer otherwise: You will only have a Stage if you have a pure JavaFx application. Please correct me if I'm wrong.
因此,要显示模态窗口,您可以传递Window实例,如代码示例所示,或传递null。
So, to display your modal window, you could pass the instance of Window as shown in the code sample or pass a null.
这篇关于如何在JavaFX中从JFXPanel打开模态对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!