Updated March 17, 2023
Introduction to JFileChooser in Java
JFileChooser is a class of swing library in Java. It is used to select a file, folder by a user. It provides a mechanism to choose a file. Application users can select & save files through this mechanism. File selection takes place in a new window dialog using JFileChooser. showOpenDialog() of JFileChooser method prompt option to user for the selection of file/folder at the specified path. JFileChooser inherits the JComponent. JComponent is also a base-level component of the swing library.
Syntax: Below given declaration below shows how a JFileChooser is used in the program.
public class JFileChooser
extends JComponent
implements Accessible
Constructor of JFileChooser in Java
JFileChooser class provides a certain parameter to set the attribute for the file selection dialog.
1. JFileChooser()
JFileChooser() constructor prompt option through a dialog for selection of the file / folder. This dialog opens at the default path. Users can select files or folders on the default path.
Syntax:
JFileChooser jFC = new JFileChooser();
2. JFileChooser(String directory path)
JFileChooser() with the string parameter as in constructor prompting a dialogue for selecting the file/folder. This dialog opens at the default path. Users can select a file or folder on the specified path as given in the constructor parameter.
Syntax:
JFileChooser jFC = new JFileChooser(String directoryPath);
3. JFileChooser(File current directory path)
JFileChooser() with the File parameter as in constructor will prompt a dialog with the specified file directory path.
Syntax:
JFileChooser jFC = new JFileChooser(File currrentDirectoryPath);
4. JFileChooser(FileSystemView)
JFileChooser() constructor with the parameter as an object in the FileSystemView prompt a dialog in the specified file-system view.
Syntax:
JFileChooser jFC = new JFileChooser(FileSystemView);
5. JFileChooser(File, FileSystemView)
JFileChooser() constructor with the parameter File path & FileSystemView as specified in the constructor prompt dialog with the specified file path & file-system view.
Syntax:
File f = new File("C:\Users\infor\Documents");
JFileChooser JFC = new JFileChooser(f, FileSystemView);
In the below-given constructors, we can see how JFileChooser prompt option is per the constructor’s parameter. These parameters provide programmers flexibility to restrict user for selection of the file & file-system view.
Advantages of JFileChooser in Java
Use of the JFileChooser has major advantages such as:
- Declaration of the JFileChooser() outside of the event listener also can be utilized in the inside of the event listener.
- JFileChooser return value, which describes whether the file has been chosen or not.
- Parameter given to the following method of JFileChooser can restrict users easily to select either file or folder or both.
Syntax:
//creating instance of the JFileChooser class
JFileChooser jFC = new JFileChooser();
jFC.setFileSelectionMode(JFileChooser.FILES_ONLY);
jFC.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jFC.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
JFileChooser also provides a method to select multiple files at once.
Syntax:
JFileChooser jfc = new JFileChooser();
//Enables multiple file selection
jfc.setMultiSelectionEnabled(true);
Examples
1. In the below-given example, JFileChooser class is one of the classes, i.e. used to select a file from the specified path. some other classes are imported into the program to handle event-related actions.
Code:
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.JFileChooser.*;
class fileSelectExample extends JFrame implements ActionListener {
static JLabel jL;
fileSelectExample(){
}
public static void main(String args[])
{
// This will be title for the frame
JFrame jF = new JFrame("File Selector");
//given width & height will set up the modal width & height
jF.setSize(420, 250);
jF.setVisible(true);
jF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//creating object of the current class
fileSelectExample fse = new fileSelectExample();
JButton buttonOpen = new JButton("open");
buttonOpen.addActionListener(fse);
JPanel jP = new JPanel();
jP.add(buttonOpen);
jL = new JLabel("Please select a fiile");
jP.add(jL);
jF.add(jP);
jF.show();
}
public void actionPerformed(ActionEvent ae)
{
String flag = ae.getActionCommand();
if (flag.equals("open")) {
JFileChooser jC = new JFileChooser();
int dialogVal = jC.showOpenDialog(null);
if (dialogVal == JFileChooser.APPROVE_OPTION)
{
jL.setText(jC.getSelectedFile().getAbsolutePath());
}
else{
jL.setText("Selection of the file cancelled!");
}
}
}
}
In the above-given example, we can see how the JFileSelector class is being used in the program.
In the above-given screenshot, A button is given to opt the file by the user; once the user clicks on the above link, it will prompt an option for the selection of the file. If multiple selections are enabled, then the user can select multiple files at once. Below given screenshot below displays how dialog opens to select the file.
Once the user select file & click on “Open” button, the chosen file remains selected as given in the following screenshot.
In the above-given screenshot, we can see how to file is chosen by the user in the dialog window. The selected file is available with the complete path & file name.
Further, if the user clicks on the “open” button & clicks on the cancel button in dialog window without a selection of any file, then the output will display the message “Selection of the file canceled”, as shown in the above screenshot.
2. In the below-given example, we can see how two methods showOpenDialog() and showSaveDialog() works. These methods are the inbuilt method of the JFileChooser class. showOpenDialog() method prompt the option to select a file, folder by a user, while the showSaveDialog () method prompts a dialog to save the file by entering the name of the file/folder.
Code:
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFileChooser.*;
class fileSaveExample extends JFrame implements ActionListener {
static JLabel jL;
fileSaveExample(){
}
public static void main(String args[])
{
// This will be title for the frame
JFrame jF = new JFrame("File Selector & Save");
//given width & height will set up the modal width & height
jF.setSize(420, 250);
jF.setVisible(true);
jF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//creating object of the current class
fileSaveExample fse = new fileSaveExample();
JButton buttonSave = new JButton("save");
JButton buttonOpen = new JButton("open");
buttonOpen.addActionListener(fse);
buttonSave.addActionListener(fse);
JPanel jP = new JPanel();
jP.add(buttonSave);
jP.add(buttonOpen);
jL = new JLabel("Please select a fiile");
jP.add(jL);
jF.add(jP);
jF.show();
}
public void actionPerformed(ActionEvent ae)
{
String flag = ae.getActionCommand();
if (flag.equals("open")) {
JFileChooser jC = new JFileChooser();
int dialogVal = jC.showOpenDialog(null);
if (dialogVal == JFileChooser.APPROVE_OPTION)
{
jL.setText(jC.getSelectedFile().getAbsolutePath());
}
else{
jL.setText("Selection of the file cancelled!");
}
}
if (flag.equals("save")) {
JFileChooser jC = new JFileChooser();
int dialogVal = jC.showSaveDialog(null);
if (dialogVal == JFileChooser.APPROVE_OPTION)
{
jL.setText(jC.getSelectedFile().getAbsolutePath());
}
else{
jL.setText("Selection of the file cancelled!");
}
}
}
}
In the above-given screenshot, we can see how to save & open a button is differentiated with each other’s action.
On click of the “open ” button, the following dialog prompt by the system where user can select file & folder
After selection of the file in the dialog, once a user clicks on the “Open” link, the selected file will be displayed in the previous window, as given in the below screenshot.
Now moving to the next step, when the user clicks on the “save” option, the following dialog will be open.
In the above-attached screenshot, we can see how a different dialog is opening with the corresponding option to open & save the file.
Conclusion
JfileChooser is one of the classes provided by the swing library to work with the file selection option. Using JFileChooser Programmers can restrict a user to view certain types of files. JFileChooser also provides some useful methods for the selection of files/folders, multiple file selection, limiting the files to the user, etc. JFileChooser also has a method to save the file by the user.
Recommended Articles
This is a guide to JFileChooser in Java. Here we discuss the introduction, Constructor of JFileChooser in Java and the Advantages of JFileChooser in Java, along with Examples. You can also go through our other suggested articles to learn more –