A JFileChooser lets the user select a file from the file system.
We can create an object of the JFileChooser class. It allows the user to choose only files, only directories, or both.
Use one of the three non-static methods, showOpenDialog(), showSaveDialog(), or showDialog(), to display it in a JDialog.
We can check for the return value, which is an int, from the method we called to show the dialog.
If it returns JFileChooser.APPROVE_OPTION, the user made a selection. JFileChooser.CANCEL_OPTION and JFileChooser.ERROR_OPTION indicate that either user cancelled the dialog box or some kind of error occurred.
To get the selected file, call the getSelectedFile() or getSelectedFiles() method, which returns a File object and a File array, respectively.
We can reuse the file chooser object. It remembers the last visited folder.
By default, a JFileChooser starts displaying files from the user's default directory.
We can specify the initial directory in its constructor or using its setCurrentDirectory() method.
The following codecCreates a file chooser with the default initial directory
JFileChooser fileChooser = new JFileChooser();
The following code creates a file chooser, with an initial directory of C:\myjava.
JFileChooser fileChooser = new JFileChooser("C:\\myjava");
By default, a file chooser can only select files.
To select files and directories
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
To allow multiple selection
fileChooser.setMultiSelectionEnabled(true);
The following code shows how to display an open file chooser dialog box and check if the user selected a file. If the user makes a selection, print the file path on the standard output.
// Display an open file chooser int returnValue = fileChooser.showOpenDialog(null);
import java.io.File; //from w w w .j a va 2 s.co m import javax.swing.JFileChooser; public class Main { public static void main(String[] args) { // Display an open file chooser JFileChooser fileChooser = new JFileChooser(); int returnValue = fileChooser.showOpenDialog(null); if (returnValue == JFileChooser.APPROVE_OPTION) { File selectedFile = fileChooser.getSelectedFile(); System.out.println("we selected: " + selectedFile); } } }
We can customize the dialog box title and the approve button text before displaying it as follows.
To change the dialog's title
fileChooser.setDialogTitle("Open a picture file");
To change the button's text
fileChooser.setApproveButtonText("Open File");
To open a file chooser with Attach as its title and approve button's text
int returnValue = fileChooser.showDialog(null, "Attach"); if (returnValue == JFileChooser.APPROVE_OPTION) { File selectedFile = fileChooser.getSelectedFile(); System.out.println("Attaching file: " + selectedFile); }
Setting the approve button's text does not change the return value of the method.
A JFileChooser can use a file filter which is a set of criteria that it applies before it shows a file in the dialog box.
A file filter is an object of the FileFilter class, which is in the javax.swing.filechooser package.
The FileFilter class is an abstract class. We need to override the accept() and getDescription() methods.
The accept() method is called with a file reference when the file chooser wants to show a file. If the accept() method returns true, the file is shown. Otherwise, the file is not shown.
The following code creates and sets a file filter to only show either a directory or a file with a doc extension.
import java.io.File; //from ww w. ja v a 2 s .com import javax.swing.JFileChooser; import javax.swing.filechooser.FileFilter; public class Main { public static void main(String[] args) { // Create a file filter to show only a directory or .doc files FileFilter filter = new FileFilter() { @Override public boolean accept(File f) { if (f.isDirectory()) { return true; } String fileName = f.getName().toLowerCase(); if (fileName.endsWith(".doc")) { return true; } return false; // Reject any other files } @Override public String getDescription() { return "Word Document"; } }; // Set the file filter JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileFilter(filter); int returnValue = fileChooser.showDialog(null, "Attach"); if (returnValue == JFileChooser.APPROVE_OPTION) { // Process the file } } }
We can use the FileNameExtensionFilter class which inherits from the FileFilter class to filer files by file extension.
Its constructor accepts the file extensions and its description. The second argument is a variable length argument.
After we create an object of the FileNameExtensionFilter class, we need to call the addChoosableFileFilter() method of the file chooser to set a filter.
The following code adds "java" and "jav" as file name extension filters.
import javax.swing.JFileChooser; import javax.swing.filechooser.FileNameExtensionFilter; /*from w w w.j av a2 s.c om*/ public class Main { public static void main(String[] args) { FileNameExtensionFilter extFilter = new FileNameExtensionFilter( "Java Source File", "java", "jav"); // Set the file filter JFileChooser fileChooser = new JFileChooser(); fileChooser.addChoosableFileFilter(extFilter); int returnValue = fileChooser.showDialog(null, "Attach"); if (returnValue == JFileChooser.APPROVE_OPTION) { // Process the file } } }
To Disable "accept all files filter"
fileChooser.setAcceptAllFileFilterUsed(false);
To check if "accept all files filter" is enabled, use the isAcceptAllFileFilterUsed() method, which returns true if a file chooser is using this filter.
To get the reference of "accept all files filter" using the getAcceptAllFileFilter() method.
The following code sets the "accept all files filter" if it is not already set.
if (!fileChooser.isAcceptAllFileFilterUsed()) {
fileChooser.setAcceptAllFileFilterUsed(true);
}
To get the associated icon for a file type, use the file chooser's getIcon(java.io.File file) method, which returns an Icon object. note that we can display an Icon object using a JLabel component.