DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Native File Dialogs in Swing - An Experiment With DJ Native Swing

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

Christopher Deckers user avatar by
Christopher Deckers
·
Mar. 30, 09 · Java Zone · News
Like (0)
Save
Tweet
24.92K Views

Join the DZone community and get the full member experience.

Join For Free

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();
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

 

Dialog (software)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Test JavaScript Code in a Browser
  • Fintech and AI: Ways Artificial Intelligence Is Used in Finance
  • 6 Things Startups Can Do to Avoid Tech Debt
  • How to Hash, Salt, and Verify Passwords in NodeJS, Python, Golang, and Java

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo