本文介绍了如何在 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.1The 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_MODALAPPLICATION_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 8ControlsFX,如果它们符合您的要求,我建议使用它们而不是开发自己的.那些内置的 JavaFX DialogAlert 类也有 initOwnerinitModalityshowAndWait 方法,以便您可以根据需要为它们设置模式(请注意,默认情况下,内置对话框是应用模式).

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 中创建模态窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 12:52
查看更多