JFileChooser

Swing, part of the Java Foundation Classes, provides a much more sophisticated and useful file chooser component written in pure Java, javax.swing.JFileChooser :

public class JFileChooser extends JComponent implements Accessible

JFileChooser is not an independent, free-standing window like FileDialog. Instead, it is a component you can add to your own frame, dialog, or other container or window. You can, however, ask the JFileChooser class to create a modal dialog just for your file chooser. Figure 13.3 shows a file chooser embedded in a JFrame window with the Metal look and feel.[26] Of course, like all Swing components, the exact appearance depends on the look and feel currently selected.

A JFileChooser with the Metal look and feel

Figure 13-3. A JFileChooser with the Metal look and feel

For the most part, the file chooser works as you expect, especially if you’re accustomed to Windows. You select a file with the mouse. Double-clicking the filename or pressing the Open button returns the currently selected file. You can change which files are displayed by selecting different filters from the pop-up list of choosable file filters. All the components have tooltips to help users who are a little thrown by an unfamiliar look and feel. One difference between a Swing file chooser and a standard, native chooser may surprise you. While double-clicking on a directory will open the directory as you expect, selecting a directory ...

Get Java I/O now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.