Show system file chooser dialog?

david.reitter's Avatar


27 May, 2019 02:20 AM

How do I correctly show a file chooser dialog (that is native to the system)?
JavaFX has one, but it's unclear to me whether this can be included in an extension (and how).

  1. Support Staff 1 Posted by Sean Reilly on 27 May, 2019 08:39 AM

    Sean Reilly's Avatar

    Hi David,

    To get a native file dialog you can use the java.awt.FileDIalog class. Although be warned that setting a filename filter on a file dialog when running on a mac will cause a freeze and possibly a crash on some recent versions of macOS.

    You could also use a JFileChooser to get the swing file selector. That's not "native" and is pretty awful on the mac, but I think it's a decent option on windows.


    Sean Reilly
    Developer, The Infinite Kind

  2. 2 Posted by Mike Bray (Quot... on 27 May, 2019 08:51 AM

    Mike Bray (Quote Loader Author)'s Avatar

    Hi David

    I use the following code:


      private void chooseFile() {
        String strDirectory = objParms.getDirectory();
        String strFileName="";
        if (Platform.isOSX()) {
            JFrame parentWindow = (JFrame) SwingUtilities.getWindowAncestor(this); 
               System.setProperty("", "true");
             FileDialog fwin = new FileDialog(parentWindow, "choose_file", FileDialog.LOAD);
             if (strDirectory != null && strDirectory != "") {
             strFileName =fwin.getFile();
             if (strFileName == null)
              strDirectory = fwin.getDirectory();
             if(strDirectory == null) {
                    strDirectory = "";
             else {
                         strFileName = strDirectory+strFileName;
        else {
            if (strDirectory == "" || strDirectory == null)
                objFileChooser = new JFileChooser();
                objFileChooser = new JFileChooser(strDirectory);
            objFileChooser.setFileFilter(new FileNameExtensionFilter("csv","CSV"));
            int iReturn = objFileChooser.showDialog(this, "Open File");
            if (iReturn == JFileChooser.APPROVE_OPTION) {
                fSecurities = objFileChooser.getSelectedFile();
                strFileName = fSecurities.getAbsolutePath();
                strDirectory = fSecurities.getParent();
        if (strFileName != ""); {

    ` You will need a class to determine which platform you are using. I have my own but MD has its own called Platform. You need to use a different solution for OSX.


  3. 3 Posted by david.reitter on 27 May, 2019 05:45 PM

    david.reitter's Avatar


    Thanks. For the record, here’s what I ended up with, with less dependencies. Haven’t tried on non-MacOS.

    The difficulty that prevented me from just trying this earlier was to get the parent frame (topFrame) in the code below. Is this the recommended way? I would have thought that MD would provide that somewhere for an extension.

    import java.awt.FileDialog;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    import java.awt.Window;


    Window parentWindow = javax.swing.FocusManager.getCurrentManager().getActiveWindow();
    System.setProperty("", "true");
    JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(parentWindow);
    java.awt.FileDialog fd = new java.awt.FileDialog(topFrame, "Export CSV", java.awt.FileDialog.SAVE);
    String directory = fd.getDirectory();
    String filename = fd.getFile();
    if (directory == null || filename == null)
    return; // cancelled
    File csvOutputFile = new File(directory + filename);


  4. 4 Posted by Mike Bray (Quot... on 28 May, 2019 08:31 AM

    Mike Bray (Quote Loader Author)'s Avatar

    Hi David

    The information available to extension authors is quite sparse, it is more of a try and see using the available Javadoc. I have written a few notes on what I have worked out (see The parent frame is not readily available to an extension.


  5. 5 Posted by rapscallion on 13 Jun, 2019 06:34 AM

    rapscallion's Avatar

    Although not a hang, I encountered a problem when using a filename extension filter with JFileChooser on Mac OS (Mojave) -- the dialog display did not appear at all. The JVM was still running and the UI was interactive, just no popup dialog box at all.

  6. System closed this discussion on 12 Sep, 2019 06:40 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac