Over a million developers have joined DZone.

Native File Dialogs in Swing - An Experiment With DJ Native Swing

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

Swing has made pretty good progress to look native, nevertheless there is one thing that definitely does not feel native: file and directory dialogs. There are several problems with Swing's file or directory dialog. It is not possible to select multiple files by dragging the mouse, it does not refresh when the list of files is modified externally, files lack their context menu, renaming files and folders is awkward, etc.

Following my recent release of DJ Native Swing 0.9.8, a developer contacted me and asked whether it offers a native file dialog component. I replied "no but I can investigate"... You see, the main difficulty is that such dialogs are natively modal which is an aspect I never had to deal with when integrating components like a web browser.

After a bit of investigation, I eventually managed to bridge SWT's file and directory dialog as shown on this screenshot:

From Swing's point of view, the API is pretty simple. Here is how to create a basic "save file" dialog:

JFileDialog fileDialog = new JFileDialog();
// This call blocks until the user has selected a file or canceled.
System.out.println("You selected: " + fileDialog.getSelectedFileName());

The file dialog supports multiple selection and extension filters. Here is such an example (an "open file" dialog):

JFileDialog fileDialog = new JFileDialog();
// We want 3 extension filters, and want the second choice (index 1) to be the default.
new String[] {"*.*", "*.mp3;*.avi", "*.txt;*.doc"},
new String[] {"All files", "Multimedia file (*.mp3, *.avi)", "Text document (*.txt, *.doc)"},
System.out.println("You selected: " + Arrays.toString(fileDialog.getSelectedFileNames()));

The directory dialog API is very similar. Here is a basic directory selection dialog:

JDirectoryDialog directoryDialog = new JDirectoryDialog();
System.out.println("You selected: " + directoryDialog.getSelectedDirectory());

Of course do not forget to initialize the Native Swing library in your main method:

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// Your application code goes here.

At this stage, I would love to hear what you think about it. If you want to try it to see for yourself, you can launch the demo with this WebStart link or download the DJ Native Swing 0.9.9 preview and run the demo. Both show the code too.

Note that DJ Native Swing works on Windows and Linux, but not yet on the Mac.



Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.


Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}