Java Swing | JFileChooser
JFileChooser is a part of java Swing package. The java Swing package is part of JavaTM Foundation Classes(JFC) . JFC contains many features that help in building graphical user interface in java . Java Swing provides components such as buttons, panels, dialogs, etc . JFileChooser is a easy and an effective way to prompt the user to choose a file or a directory .
In this article we will see how to use JFileChooser in java swing .
Constructors of JFileChooser are :
1. JFileChooser() – empty constructor that points to user’s default directory
Java
JFileChooser j = new JFileChooser();
j.showSaveDialog( null );
|
Output of the code snippet:
2. JFileChooser(String) – uses the given path
Java
JFileChooser j = new JFileChooser( "d:" );
j.showSaveDialog( null );
|
Output of the code snippet:
3. JFileChooser(File) – uses the given File as the path
Java
JFileChooser j = new JFileChooser( new File( "C:\\Users\\pc\\Documents\\New folder\\" ));
j.showSaveDialog( null );
|
Output of the code snippet:
4. JFileChooser(FileSystemView) – uses the given FileSystemView
Java
JFileChooser j = new JFileChooser(FileSystemView.getFileSystemView());
j.showSaveDialog( null );
|
Output of the code snippet:
5. JFileChooser(String, FileSystemView) – uses the given path and the FileSystemView
Java
JFileChooser j = new JFileChooser( "d:" , FileSystemView.getFileSystemView());
j.showSaveDialog( null );
|
Output of the code snippet:
6. JFileChooser(File, FileSystemView) – uses the given current directory and the FileSystemView
Java
File f = new File( "C:\\Users\\pc\\Documents\\New folder\\" );
JFileChooser j = new JFileChooser(f, FileSystemView.getFileSystemView());
j.showSaveDialog( null );
|
Output of the code snippet:
Note : The code given above are code snippets not the full code, the code snippets given above should be used to invoke the constructor as per the need and discretion of the programmer, the paths mentioned above are arbitrary. User should set the path according to their need.
Practical Applications of JFileChooser
The following codes will not execute in an online compiler. Please use an offline IDE
1. Creating an open or save dialog using JFileChooser
Java
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.filechooser.*;
class filechooser extends JFrame implements ActionListener {
static JLabel l;
filechooser()
{
}
public static void main(String args[])
{
JFrame f = new JFrame( "file chooser" );
f.setSize( 400 , 400 );
f.setVisible( true );
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button1 = new JButton( "save" );
JButton button2 = new JButton( "open" );
filechooser f1 = new filechooser();
button1.addActionListener(f1);
button2.addActionListener(f1);
JPanel p = new JPanel();
p.add(button1);
p.add(button2);
l = new JLabel( "no file selected" );
p.add(l);
f.add(p);
f.show();
}
public void actionPerformed(ActionEvent evt)
{
String com = evt.getActionCommand();
if (com.equals( "save" )) {
JFileChooser j = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
int r = j.showSaveDialog( null );
if (r == JFileChooser.APPROVE_OPTION)
{
l.setText(j.getSelectedFile().getAbsolutePath());
}
else
l.setText( "the user cancelled the operation" );
}
else {
JFileChooser j = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
int r = j.showOpenDialog( null );
if (r == JFileChooser.APPROVE_OPTION)
{
l.setText(j.getSelectedFile().getAbsolutePath());
}
else
l.setText( "the user cancelled the operation" );
}
}
}
|
2. Use JFileChooser to select directory only
Java
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.filechooser.*;
class filechooser extends JFrame implements ActionListener {
static JLabel l;
filechooser()
{
}
public static void main(String args[])
{
JFrame f = new JFrame( "file chooser to select directories" );
f.setSize( 400 , 400 );
f.setVisible( true );
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button1 = new JButton( "save" );
JButton button2 = new JButton( "open" );
filechooser f1 = new filechooser();
button1.addActionListener(f1);
button2.addActionListener(f1);
JPanel p = new JPanel();
p.add(button1);
p.add(button2);
l = new JLabel( "no file selected" );
p.add(l);
f.add(p);
f.show();
}
public void actionPerformed(ActionEvent evt)
{
String com = evt.getActionCommand();
if (com.equals( "save" )) {
JFileChooser j = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
j.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int r = j.showSaveDialog( null );
if (r == JFileChooser.APPROVE_OPTION) {
l.setText(j.getSelectedFile().getAbsolutePath());
}
else
l.setText( "the user cancelled the operation" );
}
else {
JFileChooser j = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
j.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int r = j.showOpenDialog( null );
if (r == JFileChooser.APPROVE_OPTION) {
l.setText(j.getSelectedFile().getAbsolutePath());
}
else
l.setText( "the user cancelled the operation" );
}
}
}
|
3. Use JFileChooser to allow multiple selection of files
Java
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.filechooser.*;
class filechooser extends JFrame implements ActionListener {
static JLabel l;
filechooser()
{
}
public static void main(String args[])
{
JFrame f = new JFrame( "file chooser to select multiple files at a time" );
f.setSize( 400 , 400 );
f.setVisible( true );
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button1 = new JButton( "save" );
JButton button2 = new JButton( "open" );
filechooser f1 = new filechooser();
button1.addActionListener(f1);
button2.addActionListener(f1);
JPanel p = new JPanel();
p.add(button1);
p.add(button2);
l = new JLabel( "no file selected" );
p.add(l);
f.add(p);
f.show();
}
public void actionPerformed(ActionEvent evt)
{
String com = evt.getActionCommand();
if (com.equals( "save" )) {
JFileChooser j = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
j.setMultiSelectionEnabled( true );
int r = j.showSaveDialog( null );
if (r == JFileChooser.APPROVE_OPTION) {
File files[] = j.getSelectedFiles();
int t = 0 ;
l.setText( "" );
while (t++ < files.length)
l.setText(l.getText() + " " + files[t - 1 ].getName());
}
else
l.setText( "the user cancelled the operation" );
}
else {
JFileChooser j = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
j.setMultiSelectionEnabled( true );
int r = j.showOpenDialog( null );
if (r == JFileChooser.APPROVE_OPTION) {
File files[] = j.getSelectedFiles();
l.setText( "" );
int t = 0 ;
while (t++ < files.length)
l.setText(l.getText() + " " + files[t - 1 ].getName());
}
else
l.setText( "the user cancelled the operation" );
}
}
}
|
4. Use JFileChooser to restrict the type of files shown to the user
Java
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.filechooser.*;
class filechooser extends JFrame implements ActionListener {
static JLabel l;
filechooser()
{
}
public static void main(String args[])
{
JFrame f = new JFrame( "file chooser" );
f.setSize( 400 , 400 );
f.setVisible( true );
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button1 = new JButton( "save" );
JButton button2 = new JButton( "open" );
filechooser f1 = new filechooser();
button1.addActionListener(f1);
button2.addActionListener(f1);
JPanel p = new JPanel();
p.add(button1);
p.add(button2);
l = new JLabel( "no file selected" );
p.add(l);
f.add(p);
f.show();
}
public void actionPerformed(ActionEvent evt)
{
String com = evt.getActionCommand();
if (com.equals( "save" )) {
JFileChooser j = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
j.setAcceptAllFileFilterUsed( false );
j.setDialogTitle( "Select a .txt file" );
FileNameExtensionFilter restrict = new FileNameExtensionFilter( "Only .txt files" , "txt" );
j.addChoosableFileFilter(restrict);
int r = j.showSaveDialog( null );
if (r == JFileChooser.APPROVE_OPTION)
{
l.setText(j.getSelectedFile().getAbsolutePath());
}
else
l.setText( "the user cancelled the operation" );
}
else {
JFileChooser j = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
j.setAcceptAllFileFilterUsed( false );
j.setDialogTitle( "Select a .txt file" );
FileNameExtensionFilter restrict = new FileNameExtensionFilter( "Only .txt files" , "txt" );
j.addChoosableFileFilter(restrict);
int r = j.showOpenDialog( null );
if (r == JFileChooser.APPROVE_OPTION) {
l.setText(j.getSelectedFile().getAbsolutePath());
}
else
l.setText( "the user cancelled the operation" );
}
}
}
|
NOTE :
you can also customize the approve button by using the function setApproveButtonText(String) . This will set the text of the approved button
to the desired text.
Last Updated :
01 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...