JFileChooser Class

Package: javax.swing

The JFileChooser class lets you display standard Open and Save dialog boxes similar to the ones you’ve seen in other GUI applications. Figure 5-4 shows an Open dialog box created by JChooser.

9781118239742-fg0504.tif

Figure 5-4

The result returned by the showOpenDialog method indicates whether the user chose to open a file or click Cancel, and the JFileChooser class provides a handy getSelectedFile method that you can use to get a File object for the file selected by the user.

tip.eps The JFileChooser class doesn’t actually open or save a file selected by the user; instead, it returns a File object for the file the user selects. Your program has the task of opening or saving the file.

Fields

Field

Description

static int CANCEL_OPTION

Returned by the showDialog method if the user cancels

static int APPROVE_OPTION

Returned by the showDialog method if the user selects a file

static int ERROR_OPTION

Returned by the showDialog method if an error occurs

static int FILES_ONLY

Used by the setFileSelection Mode method to indicate that the dialog box should show files only

static int DIRECTORIES_ONLY

Used by the setFileSelection Mode method to indicate that the dialog box should show directories only

static int FILES_AND_DIRECTORIES

Get Java For Dummies Quick Reference 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.