在swing应用程序中创建一个模态javafx.stage.FileChooser

时间:2017-11-08 16:31:47

标签: java swing user-interface javafx javafx-8

我有一个Swing应用程序,其主窗口继承了JFrame类。这个应用程序正在转换到JavaFX,所以JavaFX组件包括控制面板有很多。  我需要以相对于主JFrame的模态方式从非UI线程显示本机文件选择器。 这可以通过设置FileChooser类的所有者来实现,但需要javafx.stage.Window作为所有者。 是否存在将JFrame设为javafx FileChooserStage的所有者的行为?

2 个答案:

答案 0 :(得分:2)

解决方案可能是在JFrame关闭之前禁止以某种方式选择FileChooser

这是一些伪代码,因为我不知道那么好摆动:

  1. 以某种方式从JFrame中移除焦点(可能使用setFocusableWindowState(false)?)
  2. 打开FXApplication线程上的FileChooser
  3. 完成后(使用Thread.join()完成或者您想管理线程),将焦点恢复到JFrame

答案 1 :(得分:1)

实际上就像是:

JFrame frame = // get window 
frame.setEnabled(false); // emulate window modality

    File file = fileChooser.showOpenDialog(null);
    if (file == null || !openFile(file)) { // return true if file was opened correctly
        frame.setEnabled(true);
        frame.requestFocus(); // window looses focus after enabling
    }

所以FileChooser的行为就像模态窗口一样。缺点是需要控制所有路径以启用窗口,因此它不会永远保持禁用状态。