The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing.filechooser  [19 examples]

e887. Creating a JFileChooser Dialog

The following example creates a file chooser and displays it as first an open-file dialog and then as a save-file dialog:
    String filename = File.separator+"tmp";
    JFileChooser fc = new JFileChooser(new File(filename));
    
    // Show open dialog; this method does not return until the dialog is closed
    fc.showOpenDialog(frame);
    File selFile = fc.getSelectedFile();
    
    // Show save dialog; this method does not return until the dialog is closed
    fc.showSaveDialog(frame);
    selFile = fc.getSelectedFile();
Here is a more elaborate example that creates two buttons that create and show file chooser dialogs.
    // This action creates and shows a modal open-file dialog.
    public class OpenFileAction extends AbstractAction {
        JFrame frame;
        JFileChooser chooser;
    
        OpenFileAction(JFrame frame, JFileChooser chooser) {
            super("Open...");
            this.chooser = chooser;
            this.frame = frame;
        }
    
        public void actionPerformed(ActionEvent evt) {
            // Show dialog; this method does not return until dialog is closed
            chooser.showOpenDialog(frame);
    
            // Get the selected file
            File file = chooser.getSelectedFile();
        }
    };
    
    // This action creates and shows a modal save-file dialog.
    public class SaveFileAction extends AbstractAction {
        JFileChooser chooser;
        JFrame frame;
    
        SaveFileAction(JFrame frame, JFileChooser chooser) {
            super("Save As...");
            this.chooser = chooser;
            this.frame = frame;
        }
    
        public void actionPerformed(ActionEvent evt) {
            // Show dialog; this method does not return until dialog is closed
            chooser.showSaveDialog(frame);
    
            // Get the selected file
            File file = chooser.getSelectedFile();
        }
    };
Here's some code that demonstrates the use of the actions:
    JFrame frame = new JFrame();
    
    // Create a file chooser
    String filename = File.separator+"tmp";
    JFileChooser fc = new JFileChooser(new File(filename));
    
    // Create the actions
    Action openAction = new OpenFileAction(frame, fc);
    Action saveAction = new SaveFileAction(frame, fc);
    
    // Create buttons for the actions
    JButton openButton = new JButton(openAction);
    JButton saveButton = new JButton(saveAction);
    
    // Add the buttons to the frame and show the frame
    frame.getContentPane().add(openButton, BorderLayout.NORTH);
    frame.getContentPane().add(saveButton, BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true);

 Related Examples
e888. Displaying Only Directories in a File Chooser Dialog
e889. Adding a Filter to a File Chooser Dialog
e890. Determining If the Approve or Cancel Button Was Clicked in a JFileChooser Dialog
e891. Getting and Setting the Current Directory of a JFileChooser Dialog
e892. Getting and Setting the Selected File of a JFileChooser Dialog
e893. Getting the File-Type Name of a File
e894. Copying the Filename Path from a JFileChooser Dialog to the Clipboard

See also: Events    Hidden Files    Icons    Layout    Selections   


© 2002 Addison-Wesley.