Java Reference
In-Depth Information
Figure 14-10. JFileChooserDemo in action
Let's look at the code for using
JFileChooser
:
public
public class
class
JFileChooserDemo
JFileChooserDemo
extends
extends
JPanel
{
private
private static
static final
final
long
long
serialVersionUID
=
2615629432967419176L
;
/** Constructor */
public
public
JFileChooserDemo
(
JFrame f
) {
final
final
JFrame frame
=
f
;
final
final
JFileChooser chooser
=
new
new
JFileChooser
();
// If you want the user to select only directories, use this.
// Default is to allow selection of files only.
// Note if you set the selection mode to DIRECTORIES_ONLY,
// it no longer displays any files, even with the file view.
// chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
// If want it to only show certain file types, use a FileFilter.
// N.B. JFileFilter is not in javax.swing; it is my implementation
// of interface javax.swing.filechooser.FileFilter, and is similar