简体   繁体   中英

Create a modal javafx.stage.FileChooser inside a swing application

I have a Swing application the main window of which inherits JFrame class. This app is on transition to JavaFX so there is a lot on JavaFX components including control panels. I need to show native file chooser from non UI thread in modal way relatively to main JFrame . This could be achieved by setting an owner of FileChooser class, but it requires javafx.stage.Window to be the owner. Is there some hack to set JFrame as owner of javafx FileChooser or Stage ?

The solution may be to disable selecting your JFrame somehow until the FileChooser closes.

Here's some pseudocode, since I don't know swing all that well:

  1. remove focus from the JFrame somehow (perhaps using setFocusableWindowState(false) ?)
  2. open the FileChooser on the FXApplication thread
  3. after that finishes (done using Thread.join() or however you want to manage your threads), restore focus to the JFrame

Actually it was like just:

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
    }

So FileChooser will behave like modal window here. The downside is there's need to control all paths to enable window back so it will not stay disabled forever.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM