*具有* FXML的JavaFX 2.2模态窗口对话框 [英] JavaFX 2.2 Modal window dialog *with* FXML

查看:364
本文介绍了*具有* FXML的JavaFX 2.2模态窗口对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用@jewelsea提供的示例在此要点,但我陷入了困境因为我正在使用FXML.

I am trying to use the example provided by @jewelsea at this gist and I'm stuck because I'm using FXML.

我看过这两个帖子:如何在JavaFX 2.1和此JavaFX 2模态窗口的答案中创建模态窗口.

I have seen both of these posts: How to create a modal window in JavaFX 2.1 and this answer to JavaFX 2 modal window.

我困在的地方是jewelsea的代码中,上面写着:

Where I'm stuck is in the code by jewelsea, where it says:

final WebView webView = new WebView(); 
webView.getEngine().load("http://docs.oracle.com/javafx/");
primaryStage.setScene(new Scene(webView));

由于我使用的是FXML,因此我这样做:

Whereas, since I'm using FXML, I do this:

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("MyFXML.fxml"));
Scene scene = (Scene)fxmlLoader.load();
myController = fxmlLoader.getController();
primaryStage.setScene(scene);

您能告诉我如何修改我的代码(上面的4行),使其与jewelsea的示例兼容吗?

Can you tell me how to modify my code (the 4 lines above), so it works with jewelsea's example?

谢谢!

推荐答案

通过FXMLLoader而不是WebView创建的场景示例代码使用的替换代码很好,您无需进行修改.

The replacement code you are using via FXMLLoader rather than the WebView created scene sample code is fine, you don't need to modify it.

您的代码将基于fxml文档显示主要阶段的主要场景(对于我的示例,我使用WebView作为我的主要场景,因此不需要,因此不需要任何WebView要点的相关代码).

Your code will display the main scene for your primary stage based on an fxml document (for my sample I was using a WebView as my main scene, which you don't need, so you don't need any of the WebView related code from the gist).

您需要的是在您想要显示对话框时控制器中的一些触发器.举一个简单的例子,您可以为主要场景设置一个fxml,其中仅包含一个按钮,然后在控制器中为该按钮提供一个ActionHandler(就像

What you need is some trigger in your controller at the time you want to display a dialog. As a simple example, you can setup an fxml for your main scene which just includes a button, and then supply an ActionHandler for the button in your controller (just like in the Introduction to FXML document).

现在,不仅仅是像FXML简介中那样在按下按钮时仅执行println,而是像在WebView文档加载时的要点一样,调用dialog.show()方法.现在应该发生的是,该对话框现在将显示在fxml生成的场景的顶部.

Now, instead of just doing a println when the button is pressed as Introduction to FXML has, call the dialog.show() method like the gist did at the time of WebView document load. What should happen is that the dialog will now be displayed on top of your fxml generated scene.

您会注意到对话框本身包含一个场景.如果需要(并且这是可选的),则可以使用fxml定义场景内容.为此,在构建对话框时,您需要为对话框内容设置新的fxml和新控制器,并将为对话框创建的fxml创建场景加载到对话框的阶段.用于执行此操作的代码与用于将主场景的fxml加载到primaryStage中的代码几乎相同.

You will notice that the dialog itself contains a Scene. If you wanted (and this is optional), you could define the scene contents using fxml. To do this, at the time of dialog construction, you set up a new fxml and new controller for the dialog contents and load the fxml created scene for the dialog into the dialog's stage. The code to do this is pretty much identical code to what you used to load the main scene's fxml into the primaryStage.

尝试上面的方法,看看它是否适合您.如果您仍然遇到问题,我可能可以按照此答案创建一个fxml的gist特定示例.

Try the above and see if it works for you. If you are still stuck I can probably create an fxml specific example of the gist along the lines of this answer.

还要注意,所引用的要点是在前一段时间写的,现在有一个

Also note that the referenced gist was written a while ago, and there is a now a showAndWait method in JavaFX which facilitates blocking execution of code when making a dialog call and then allowing to process the result of the dialog without using some of the event handler mechanisms from the gist example. Strategies both with and without showAndWait are perfectly acceptable solutions though.

这篇关于*具有* FXML的JavaFX 2.2模态窗口对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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