DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Security Challenges for Microservice Applications in Multi-Cloud Environments
  • Azure Virtual Machines
  • What Is JHipster?
  • Tech Hiring: Trends, Predictions, and Strategies for Success

Trending

  • Security Challenges for Microservice Applications in Multi-Cloud Environments
  • Azure Virtual Machines
  • What Is JHipster?
  • Tech Hiring: Trends, Predictions, and Strategies for Success

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

Christopher Deckers user avatar by
Christopher Deckers
·
Mar. 30, 09 · News
Like (0)
Save
Tweet
Share
25.35K 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.

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

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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

Let's be friends: