如何在JavaFX 2.1中创建模态窗口 [英] How to create a modal window in JavaFX 2.1

查看:172
本文介绍了如何在JavaFX 2.1中创建模态窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何在JavaFX中创建模态窗口。基本上我有文件选择器,我想在用户选择文件时询问用户。我需要这些信息才能解析文件,因此执行需要等待答案。

I can't figure out how to create a modal window in JavaFX. Basically I have file chooser and I want to ask the user a question when they select a file. I need this information in order to parse the file, so the execution needs to wait for the answer.

我见过这个问题但是我无法找到如何实现这种行为。

I've seen this question but I've not been able to find out how to implement this behavior.

推荐答案

以下是链接到解决方案我之前为JavaFX 2.1中的模态对话框创建了一个解决方案
该解决方案在当前阶段的顶部创建一个模态阶段,并通过对话框控件的事件处理程序对对话框结果执行操作。

Here is link to a solution I created earlier for modal dialogs in JavaFX 2.1 The solution creates a modal stage on top of the current stage and takes action on the dialog results via event handlers for the dialog controls.

更新

之前的链接解决方案使用过时的事件处理程序方法来执行操作对话被驳回了。该方法适用于JavaFX之前的2.2实现。对于JavaFX 8+,不需要事件处理程序,而是使用新的Stage showAndWait() 方法。例如:

The prior linked solution uses a dated event handler approach to take action after a dialog was dismissed. That approach was valid for pre-JavaFX 2.2 implementations. For JavaFX 8+ there is no need for event handers, instead, use the new Stage showAndWait() method. For example:

Stage dialog = new Stage();

// populate dialog with controls.
...

dialog.initOwner(parentStage);
dialog.initModality(Modality.APPLICATION_MODAL); 
dialog.showAndWait();

// process result of dialog operation. 
... 

请注意,为了使事情按预期工作,它是初始化舞台的所有者以将舞台的模态初始化为 WINDOW_MODAL APPLICATION_MODAL

Note that, in order for things to work as expected, it is important to initialize the owner of the Stage and to initialize the modality of the Stage to either WINDOW_MODAL or APPLICATION_MODAL.

JavaFX 8 ControlsFX ,如果它们符合您的要求,我建议使用这些而不是开发自己的。那些内置的JavaFX Dialog 警报类也有 initOwner initModality showAndWait 方法,以便您可以设置你想要的模态(注意,默认情况下,内置的对话框是应用程序模态)。

There are some high quality standard UI dialogs in JavaFX 8 and ControlsFX, if they fit your requirements, I advise using those rather than developing your own. Those in-built JavaFX Dialog and Alert classes also have initOwner and initModality and showAndWait methods, so that you can set the modality for them as you wish (note that, by default, the in-built dialogs are application modal).

这篇关于如何在JavaFX 2.1中创建模态窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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