tag:infinitekind.tenderapp.com,2009-01-14:/discussions/moneydance-development/2468-show-system-file-chooser-dialogInfinite Kind: Discussion 2019-09-12T06:40:23Ztag:infinitekind.tenderapp.com,2009-01-14:Comment/473002252019-05-27T02:20:14Z2019-05-27T02:20:14ZShow system file chooser dialog?<div><p>How do I correctly show a file chooser dialog (that is native to the system)?<br>
JavaFX has one, but it's unclear to me whether this can be included in an extension (and how).</p></div>david.reittertag:infinitekind.tenderapp.com,2009-01-14:Comment/473002252019-05-27T08:39:39Z2019-05-27T08:39:39ZShow system file chooser dialog?<div><p>Hi David,</p>
<p>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.</p>
<p>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.</p>
<p>Thanks,<br>
Sean</p>
<p>--<br>
Sean Reilly<br>
Developer, The Infinite Kind<br>
<a href="https://infinitekind.com">https://infinitekind.com</a></p></div>Sean Reillytag:infinitekind.tenderapp.com,2009-01-14:Comment/473002252019-05-27T08:51:18Z2019-05-27T08:51:18ZShow system file chooser dialog?<div><p>Hi David</p>
<p>I use the following code:</p>
<p>`</p>
<pre>
<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();
}
}</code>
</pre>
<p>` 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.</p>
<p>Mike</p></div>Mike Bray (Quote Loader Author)tag:infinitekind.tenderapp.com,2009-01-14:Comment/473002252019-05-27T17:45:06Z2019-05-27T17:45:06ZShow system file chooser dialog?<div><p>Sean/Mike,</p>
<p>Thanks. For the record, here’s what I ended up with, with less dependencies. Haven’t tried on non-MacOS.</p>
<p>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.</p>
<p>import java.awt.FileDialog;<br>
import javax.swing.JFrame;<br>
import javax.swing.SwingUtilities;<br>
import java.awt.Window;</p>
<p>...</p>
<pre>
<code>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);</code>
</pre>
<p>David</p></div>david.reittertag:infinitekind.tenderapp.com,2009-01-14:Comment/473002252019-05-28T08:31:58Z2019-05-28T08:31:58ZShow system file chooser dialog?<div><p>Hi David</p>
<p>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 <a href="https://bitbucket.org/mikerb/moneydance-2019/wiki/Home">https://bitbucket.org/mikerb/moneydance-2019/wiki/Home</a>). The parent frame is not readily available to an extension.</p>
<p>Regards<br>
Mike</p></div>Mike Bray (Quote Loader Author)tag:infinitekind.tenderapp.com,2009-01-14:Comment/473002252019-06-13T06:34:37Z2019-06-13T06:44:25ZShow system file chooser dialog?<div><p>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.</p></div>rapscallion