Show system file chooser dialog?

david.reitter's Avatar

david.reitter

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.

    Thanks,
    Sean

    --
    Sean Reilly
    Developer, The Infinite Kind
    https://infinitekind.com

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

    Mike Bray '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("com.apple.macos.use-file-dialog-packages", "true");
             FileDialog fwin = new FileDialog(parentWindow, "choose_file", FileDialog.LOAD);
             if (strDirectory != null && strDirectory != "") {
                    fwin.setDirectory(strDirectory);
             }
             fwin.setVisible(true);
    
             strFileName =fwin.getFile();
             if (strFileName == null)
                 return;
              strDirectory = fwin.getDirectory();
             if(strDirectory == null) {
                    strDirectory = "";
             }
             else {
                         strFileName = strDirectory+strFileName;
             }
    
           }
        else {
            if (strDirectory == "" || strDirectory == null)
                objFileChooser = new JFileChooser();
            else
                objFileChooser = new JFileChooser(strDirectory);
            objFileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            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 != ""); {
            txtFileName.setText(strFileName);
            objParms.setDirectory(strDirectory);
            objParms.save();    
            loadFields();
        }
    }
    

    ` 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.

    Mike

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

    david.reitter's Avatar

    Sean/Mike,

    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("com.apple.macos.use-file-dialog-packages", "true");
    JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(parentWindow);
    java.awt.FileDialog fd = new java.awt.FileDialog(topFrame, "Export CSV", java.awt.FileDialog.SAVE);
    fd.setVisible(true);
    String directory = fd.getDirectory();
    String filename = fd.getFile();
    if (directory == null || filename == null)
    return; // cancelled
    File csvOutputFile = new File(directory + filename);

    David

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

    Mike Bray '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 https://bitbucket.org/mikerb/moneydance-2019/wiki/Home). The parent frame is not readily available to an extension.

    Regards
    Mike

  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.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? 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

Recent Discussions

15 Jul, 2019 07:21 PM
15 Jul, 2019 06:34 PM
15 Jul, 2019 05:29 PM
15 Jul, 2019 04:37 PM
15 Jul, 2019 04:27 PM