Native File Dialogs in Swing - An Experiment With DJ Native Swing
Join the DZone community and get the full member experience.
Join For FreeSwing 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();
fileDialog.setDialogType(DialogType.SAVE_DIALOG_TYPE);
// This call blocks until the user has selected a file or canceled.
fileDialog.show(parentWindow);
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();
fileDialog.setSelectionMode(SelectionMode.MULTIPLE_SELECTION);
// We want 3 extension filters, and want the second choice (index 1) to be the default.
fileDialog.setExtensionFilters(
new String[] {"*.*", "*.mp3;*.avi", "*.txt;*.doc"},
new String[] {"All files", "Multimedia file (*.mp3, *.avi)", "Text document (*.txt, *.doc)"},
1);
fileDialog.show(parentWindow);
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();
directoryDialog.show(parentWindow);
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) {
NativeInterface.open();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// Your application code goes here.
}
});
NativeInterface.runEventPump();
}
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.
-Christopher
Opinions expressed by DZone contributors are their own.
Trending
-
Security Challenges for Microservice Applications in Multi-Cloud Environments
-
Azure Virtual Machines
-
What Is JHipster?
-
Tech Hiring: Trends, Predictions, and Strategies for Success
Comments