java-当FileChooser处于活动状态时,防止窗口聚焦

我有一个简单的JavaFX应用程序,仅通过调用showOpenDialog()即可打开FileChooser.

我想在打开文件选择器时禁止选择我的主窗口,并在可能的情况下将其保留在主窗口的顶部.

感谢您提供的任何帮助.

最佳答案
从文档showOpenDialog(重点是我的):

Shows a new file open dialog. The method doesn’t return until the
displayed open dialog is dismissed. The return value specifies the
file chosen by the user or null if no selection has been made. If the
owner window for the file dialog is set, input to all windows in the
dialog’s owner chain is blocked while the file dialog is being shown.

因此,由于拥有者链,在这种情况下,primaryStage和secondStage均被阻止:

primaryStage.setScene(new Scene(new VBox(), 300, 300));
primaryStage.show();

Stage secondStage = new Stage();
secondStage.setScene(new Scene(new VBox(), 50, 50));
secondStage.initOwner(primaryStage);

secondStage.show();

FileChooser fc = new FileChooser();
fc.showOpenDialog(secondStage);

如果您将最后一行修改为

fc.showOpenDialog(primaryStage);

primaryStage被阻止,但secondStage可用.

最后,如果不执行此行:

secondStage.initOwner(primaryStage);

然后将最后一行称为

fc.showOpenDialog(secondStage);

primaryStage没有被阻止,但是secondStage被阻止了.

转载注明原文:java-当FileChooser处于活动状态时,防止窗口聚焦 - 代码日志