JFileChooser Example in Swing for Beginners

There are 6 ways to create a JFileChooser. Yes, you heard it right. But what does that mean? I mean, you can create them using either of its six constructors and then mold the things the way you want with the simple methods. The following example seems to be big, but if you tackle each statement, you could easily understand it. I'm sure. And keeping my motto, I coded it in the easy way with helpful commentaries at the top of each statement.

import javax.swing.*;
import javax.swing.filechooser.*;
import java.awt.*;
import java.io.*;
class JFileChooserDemo extends JFrame
{
JFileChooser jf1,jf2,jf3,jf4,jf5,jf6;
FileSystemView f=FileSystemView.getFileSystemView();
File dir=new File("E:\\java");
javax.swing.border.Border b=BorderFactory.createLineBorder(Color.GRAY,1,false);
FileNameExtensionFilter imageFilter=new FileNameExtensionFilter("Image Files","jpg","jpeg","png","gif");

    public JFileChooserDemo()
    {
        createAndShowGUI();
    }
   
    private void createAndShowGUI()
    {
        setTitle("JFileChooser Example");
        setLayout(new GridLayout(3,2));
        setDefaultCloseOperation(EXIT_ON_CLOSE);
       
        jf1=new JFileChooser();
        // dir
        jf2=new JFileChooser(dir);
        // dir and FileSystemView
        jf3=new JFileChooser(dir,f);
        // FileSystemView
        jf4=new JFileChooser(f);
        // Current dir as String
        jf5=new JFileChooser("E:\\java");
        // Current dir path, FileSystemView
        jf6=new JFileChooser("E:\\java",f);
       
        jf1.setBorder(b);
        jf2.setBorder(b);
        jf3.setBorder(b);
        jf4.setBorder(b);
        jf5.setBorder(b);
        jf6.setBorder(b);
       
        jf1.addChoosableFileFilter(imageFilter);
        jf1.setCurrentDirectory(dir);
       
        // This sets the current file filter to image filter
        jf2.setFileFilter(imageFilter);
       
        // To disable all files filter
        jf2.setAcceptAllFileFilterUsed(false);
       
        // Set multiple selection enabled
        // default is false
        jf2.setMultiSelectionEnabled(true);
       
        // Set file hiding disabled, show hidden files
        // The default value depends upon your settings in OS
        jf1.setFileHidingEnabled(false);
       
        // Hide approve and cancel buttons
        jf3.setControlButtonsAreShown(false);
       
        // default is OPEN_DIALOG
        jf1.setDialogType(JFileChooser.SAVE_DIALOG);
        jf2.setDialogType(JFileChooser.CUSTOM_DIALOG);
       
        jf2.setApproveButtonText("Button");
        jf1.setApproveButtonToolTipText("Click to save");
       
        jf2.setApproveButtonMnemonic('B');
       
        // set file selection mode FILES_ONLY or DIRECTORIES_ONLY
        // or FILES_AND_DIRECTORIES
        // default is FILES_ONLY
        jf3.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
       
        // Set a selected file
        jf1.setSelectedFile(new File("E:\\java\\JFileChooserDemo.java"));
       
        // For multiple selection
        // jf2.setSelectedFiles(File[]);
        // getSelectedFiles() gets the selected files
       
        System.out.println("Selected file in jf1 is "+jf1.getSelectedFile());
       
        // Set accessory (an additional component)
        // Must be called before JFileChooser is shown
        // i.e. before showing dialog or adding
        jf1.setAccessory(new JButton("Button"));
       
        jf1.showSaveDialog(this);
       
        add(jf1);
        add(jf2);
        add(jf3);
        add(jf4);
        add(jf5);
        add(jf6);
       
        setSize(400,400);
        setVisible(true);
    }
   
    public static void main(String args[])
    {
        new JFileChooserDemo();
    }
}

Screenshot of JFileChooser Example


Posted by at on

Tags: Swing Components,