Christopher has posted 9 posts at DZone. You can read more from them at their website. View Full User Profile

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

  • submit to reddit

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.



Published at DZone with permission of its author, Christopher Deckers.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Jay Huang replied on Sun, 2009/04/26 - 11:04pm

Hi  Christopher,

Thanks for the quick response.  How do I set and get the current directory using JFileDialog ?  I thought that JFileDialog extends SWT FileDialog but it doesn't compile when I tried to use fileDialog.setFilterPath(Dir).



Christopher Deckers replied on Mon, 2009/04/27 - 4:57am in response to: Jay Huang

Hi Jay,

I think what you are looking for is setParentDirectory(String).

Hope this helps,

Jay Huang replied on Mon, 2009/04/27 - 1:08pm in response to: Christopher Deckers

Thanks Christopher. It works well.

Jay Huang replied on Tue, 2009/06/16 - 2:37pm

Hi Chris,


One more question. Is it possible to add a listener to JFileDialog so that it knows which file is be selected (clicked) on ?



Christopher Deckers replied on Wed, 2009/06/17 - 3:37am in response to: Jay Huang

Hi Jay,

Only when the dialog is closed you can perfom some action, like getting the file that was selected.
All that happens while the dialog is on screen is not visible from the application so it is not possible to add a listener.


Jay Huang replied on Wed, 2009/06/17 - 2:11pm in response to: Christopher Deckers

Thanks Chris.  I was trying to create image preview before openning the file.  Guess that I'm out of luck on this one.



Ashwathy Vish replied on Fri, 2009/12/25 - 2:08am

How to use the getHTML() function of your api? Wat object does it accept?

Christopher Deckers replied on Tue, 2009/12/29 - 6:20am in response to: Ashwathy Vish


Please have a look at the Javadoc and the demo application.


Santosh Shanbhag replied on Thu, 2010/02/18 - 10:32pm

Is djnativeswing available in a Maven repository? If so, please let me know how to specify the dependency and also the repository url.

 Also, what all I need to include. I noticed there are other jar files. Do you have any documentation on when to use which jar file? The code snippets are very useful. 

Christopher Deckers replied on Fri, 2010/02/19 - 4:05am in response to: Santosh Shanbhag

Hi Santosh,

No, it is not available in a Maven repository.

As for the actual JARs, I do not yet have a documentation on that, but you can probably experiment.

If you have further questions, please use the DJ Project forum.



Java User07 replied on Fri, 2010/07/02 - 2:12am

I have one critical issue here, Open Native Dialog, select the option open multi file selection option. Select multiple files, drag the files such that your mouse is out of that file dialog. Then It will hang. This is a major issue I can find in it. What can be the cause for it. Is it not able to recognise the mouse event in dll if focus moved out of dialog if drag the mouse out of dialog. Thanks in advance.

Christopher Deckers replied on Mon, 2010/07/05 - 12:40pm in response to: Java User07


The issue is that initiating a drag freezes the dialog, it does not have to be in multi file mode.

I don't know how to fix it because Swing is blocked until the native dialog is closed, but for some reasons the drag operation is waiting forever.

I'll try to find a solution but I don't have any ideas as of now.

By-the-way, please use the user forum on the SourceForge project page for further comments/questions.


Christopher Deckers replied on Sun, 2010/07/11 - 8:39am in response to: Java User07


I worked on it a little, and it should be fixed now.
It is part of the latest 0.9.9 preview version, available on the SourceForge download page (the one tagged 20100711)

Hope this helps,

Java User07 replied on Mon, 2010/07/26 - 3:35am

Hi, I need some more information on this Native dialogs. How to set Locale to this Native dialog. Like If I am setting the Locale to some language JFileChooser dialog will open display in that Language[All buttons and Lables]. How to get the same kind of functionality here. Is there any way to set the Locale, I tried for some time but couldn't get any solution. Can you help me on this one. Thanks in advance.

Christopher Deckers replied on Mon, 2010/07/26 - 8:01am in response to: Java User07


as far as I know, the native dialogs use the system's language and there is no way to change that.

Hope this helps,

Java User07 replied on Mon, 2010/07/26 - 11:49pm in response to: Christopher Deckers

Thanks for your immediate reply on this. I feel this is a kind of supressing the functinality provided by Java file browser? If am planning to move from Java file browser to this native file browser, I can't use the Language based selection browser. So how did you feel? Is this a considaration point?

Christopher Deckers replied on Tue, 2010/07/27 - 4:45am in response to: Java User07

I have no control over the native file dialog from the operating system: it has the language set for the system.

So in a way you lose language selection, but you get all the operations that the native file dialog allows (drag select, renaming, file copying, etc).

My view is that if the OS is in a given language, then the user expects all its environment to be in this language. Therefore, if an application is not in the language of the OS, it is the fault of the application and not of the few components that respect the language of the OS. I understand that proper i18n in an application has a cost and so is not always possible, but that is how it should be anyway.

Hope this helps,

Java User07 replied on Tue, 2010/07/27 - 8:21am in response to: Christopher Deckers

Yes I do agree with you, The Application that we are working is supporting multi language selection. If the system language and selected language in the application is same then the Native file browser is showing properly as expected. My scenario is cross language selection. I raised this point by considering the Java File Browser. I am not proposing as we should implement this future. I am checking with the posibilities. So any way I got conclusion, for changing the language to Native dailogs there is no posibility. Thanks Christopher. Sugession: It will be good if we provide one method to set the default file name for Save File Dialog browser. If default file name is given on clicking Save dialog, default file name will be displayed in the file name field. If he wantks he can change the name as well.

Christopher Deckers replied on Tue, 2010/07/27 - 9:32am in response to: Java User07


> Sugession: It will be good if we provide one method to set the default file name for Save File Dialog browser.

I think this method is what you are looking for:


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.