I am trying to use the native Windows file dialog in Java, using JNA to call the comdlg32 GetOpenFileName function. I made a static OpenFileDialog.display method that looks like this:
public static List<File> display(Window parent, boolean allowMultiSelect)
It should return the selected files, or null if the user canceled the dialog.
I have two simple test programs. This works as expected:
package nativedialogs; import java.io.File; import java.util.List; public class SimpleTest { public static void main(String[] args) { List<File> files = OpenFileDialog.display(null, true); System.out.println(files); } }
This, however, does not mean:
package nativedialogs; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.util.List; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.SwingUtilities; public class SwingTest { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Open file dialog"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { List<File> files = OpenFileDialog.display(f, true);
In the last example, CommDlgExtendedError returns 2, which according to MSDN :
CDERR_INITIALIZATION 0x0002
During initialization, the function of the general dialog box failed. This error often occurs when there is not enough memory.
... which really doesn't help me much. What am I doing wrong here?
I put other sources in the PasteBin so that I donβt confuse the question too much:
OpenFileDialog : http://pastebin.com/HDmu0TjX
ComDlg32JNA : http://pastebin.com/X5F5LLip