Using the JFileChooser in Java

JFileChooser in Java is used to select a file both for opening a file and also saving it. Here is an example that demonstrates it’s use.

 

 

package jfilechooser;

 

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class JFileChooserDemo extends JFrame implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
String ac=evt.getActionCommand();
if(ac.equals(“Open”))
{
int result=chooser.showOpenDialog(this);
if(result!=JFileChooser.APPROVE_OPTION)
{
txtopen.setText(“Cancel was selected”);
return;
}
File selectedfile=chooser.getSelectedFile();
txtopen.setText(“” + selectedfile);
}
if(ac.equals(“Save”))
{
int result=chooser.showSaveDialog(this);
if(result!=JFileChooser.APPROVE_OPTION)
{
txtsave.setText(“Cancel was selected”);
return;
}
File selectedfile=chooser.getSelectedFile();
txtsave.setText(“” + selectedfile);
}
}
private JTextField txtopen,txtsave;
private JFileChooser chooser=new JFileChooser();
public JFileChooserDemo()
{
txtopen=new JTextField(50);
txtsave=new JTextField(50);
setLayout(new FlowLayout());
setSize(700,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JButton bttnopen=new JButton(“Open”);
JButton bttnsave=new JButton(“Save”);
add(new JLabel(“Open”));
add(txtopen);
add(new JLabel(“Save”));
add(txtsave);
add(bttnopen);
add(bttnsave);
bttnopen.addActionListener(this);
bttnsave.addActionListener(this);
}
public static void main(String[] args)
{
JFileChooserDemo f=new JFileChooserDemo();
f.setVisible(true);
}
}

JFileChooser frame

Press the Open Button

JFileChooser Open Dialog

File selected through JFileChooser Open Dialog

JFileChoose Save File Dialog

JFileChooser Save File Path

FileFilter: To control the types of files visible in the file chooser dialog we can devise a file filter.
The filter class would extend javax.swing.filechooser.FileFilter.
The class has two important methods
1. public boolean accept(java.io.File file)
If this returns true the file is displayed, otherwise not.
2. The other is a method called public String getDescription()
that shows the description for the file chooser.

Here is code that would display png, jpg and gif files.

import javax.swing.filechooser.FileFilter;
public class DemoFilter extends FileFilter
{
public boolean accept(java.io.File file)
{
if(file.isDirectory())
return true;
String name=file.getName().toLowerCase().trim();
if(name.endsWith(“.png”) || name.endsWith(“.jpg”) || name.endsWith(“.gif”))
return true;
else
return false;
}
public String getDescription()
{
return “Pictures”;
}
}

To add the filter to the JFileChooser we write:
chooser.setFileFilter(new DemoFilter());

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class JFileChooserDemo extends JFrame implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
String ac=evt.getActionCommand();
if(ac.equals(“Open”))
{
int result=chooser.showOpenDialog(this);
if(result!=JFileChooser.APPROVE_OPTION)
{
txtopen.setText(“Cancel was selected”);
return;
}
File selectedfile=chooser.getSelectedFile();
txtopen.setText(“” + selectedfile);
}
if(ac.equals(“Save”))
{
int result=chooser.showSaveDialog(this);
if(result!=JFileChooser.APPROVE_OPTION)
{
txtsave.setText(“Cancel was selected”);
return;
}
File selectedfile=chooser.getSelectedFile();
txtsave.setText(“” + selectedfile);
}
}
private JTextField txtopen,txtsave;
private JFileChooser chooser=new JFileChooser();
public JFileChooserDemo()
{
chooser.setFileFilter(new DemoFilter());
txtopen=new JTextField(50);
txtsave=new JTextField(50);
setLayout(new FlowLayout());
setSize(700,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JButton bttnopen=new JButton(“Open”);
JButton bttnsave=new JButton(“Save”);
add(new JLabel(“Open”));
add(txtopen);
add(new JLabel(“Save”));
add(txtsave);
add(bttnopen);
add(bttnsave);
bttnopen.addActionListener(this);
bttnsave.addActionListener(this);
}
public static void main(String[] args)
{
JFileChooserDemo f=new JFileChooserDemo();
f.setVisible(true);
}
}

Now, we have two file filters.
1. Pictures

file filter

2. All Files

file filter for all files

 

To remove the All Files option we need to get the all files filter. This is done by calling the getAcceptAllFileFilter(). To remove it use the removeChoosableFileFilter method. The full code would be:
chooser.removeChoosableFileFilter( chooser.getAcceptAllFileFilter());

 

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class JFileChooserDemo extends JFrame implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
String ac=evt.getActionCommand();
if(ac.equals(“Open”))
{
int result=chooser.showOpenDialog(this);
if(result!=JFileChooser.APPROVE_OPTION)
{
txtopen.setText(“Cancel was selected”);
return;
}
File selectedfile=chooser.getSelectedFile();
txtopen.setText(“” + selectedfile);
}
if(ac.equals(“Save”))
{
int result=chooser.showSaveDialog(this);
if(result!=JFileChooser.APPROVE_OPTION)
{
txtsave.setText(“Cancel was selected”);
return;
}
File selectedfile=chooser.getSelectedFile();
txtsave.setText(“” + selectedfile);
}
}
private JTextField txtopen,txtsave;
private JFileChooser chooser=new JFileChooser();
public JFileChooserDemo()
{
chooser.removeChoosableFileFilter( chooser.getAcceptAllFileFilter());
chooser.setFileFilter(new DemoFilter());
txtopen=new JTextField(50);
txtsave=new JTextField(50);
setLayout(new FlowLayout());
setSize(700,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JButton bttnopen=new JButton(“Open”);
JButton bttnsave=new JButton(“Save”);
add(new JLabel(“Open”));
add(txtopen);
add(new JLabel(“Save”));
add(txtsave);
add(bttnopen);
add(bttnsave);
bttnopen.addActionListener(this);
bttnsave.addActionListener(this);
}
public static void main(String[] args)
{
JFileChooserDemo f=new JFileChooserDemo();
f.setVisible(true);
}
}

file filter without all files

Adding more filters.
To do this you create many filters and add using ChooseableFileFilters.

PngFilter

 

import javax.swing.filechooser.FileFilter;
public class PngFilter extends FileFilter

{
public boolean accept(java.io.File file)
{
if(file.isDirectory())
return true;
String name=file.getName().toLowerCase().trim();
if(name.endsWith(“.png”))
return true;
else
return false;
}
public String getDescription()
{
return “Png”;
}
}

JpgFilter

 

import javax.swing.filechooser.FileFilter;
public class JpgFilter extends FileFilter

{
public boolean accept(java.io.File file)
{
if(file.isDirectory())
return true;
String name=file.getName().toLowerCase().trim();
if(name.endsWith(“.jpg”))
return true;
else
return false;
}
public String getDescription()
{
return “Jpg”;
}
}

The main program now:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class JFileChooserDemo extends JFrame implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
String ac=evt.getActionCommand();
if(ac.equals(“Open”))
{
int result=chooser.showOpenDialog(this);
if(result!=JFileChooser.APPROVE_OPTION)
{
txtopen.setText(“Cancel was selected”);
return;
}
File selectedfile=chooser.getSelectedFile();
txtopen.setText(“” + selectedfile);
}
if(ac.equals(“Save”))
{
int result=chooser.showSaveDialog(this);
if(result!=JFileChooser.APPROVE_OPTION)
{
txtsave.setText(“Cancel was selected”);
return;
}
File selectedfile=chooser.getSelectedFile();
txtsave.setText(“” + selectedfile);
}
}
private JTextField txtopen,txtsave;
private JFileChooser chooser=new JFileChooser();
public JFileChooserDemo()
{
chooser.removeChoosableFileFilter( chooser.getAcceptAllFileFilter());
chooser.addChoosableFileFilter(new PngFilter());
chooser.addChoosableFileFilter(new JpgFilter());
txtopen=new JTextField(50);
txtsave=new JTextField(50);
setLayout(new FlowLayout());
setSize(700,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JButton bttnopen=new JButton(“Open”);
JButton bttnsave=new JButton(“Save”);
add(new JLabel(“Open”));
add(txtopen);
add(new JLabel(“Save”));
add(txtsave);
add(bttnopen);
add(bttnsave);
bttnopen.addActionListener(this);
bttnsave.addActionListener(this);
}
public static void main(String[] args)
{
JFileChooserDemo f=new JFileChooserDemo();
f.setVisible(true);
}
}

png filter

jpg filter

 

To change the file icons we can use our own custom pictures. Here are the three pictures we will use.

folder icon jpg icon png icon
Here is the code for the file view:

 

 

import java.io.File;
import javax.swing.Icon;

import javax.swing.ImageIcon;
import javax.swing.filechooser.FileView;

public class DemoView extends FileView
{
public Icon getIcon(File file)
{
if(file.isDirectory())
return new ImageIcon(“e:\\folder.png”);
String name=file.getName().toLowerCase().trim();
if(name.endsWith(“png”))
return new ImageIcon(“e:\\png.png”);
if(name.endsWith(“jpg”))
return new ImageIcon(“e:\\jpg.png”);
return new ImageIcon(“e:\\folder.png”);
}
}

The complete code

 

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class JFileChooserDemo extends JFrame implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
String ac=evt.getActionCommand();
if(ac.equals(“Open”))
{
int result=chooser.showOpenDialog(this);
if(result!=JFileChooser.APPROVE_OPTION)
{
txtopen.setText(“Cancel was selected”);
return;
}
File selectedfile=chooser.getSelectedFile();
txtopen.setText(“” + selectedfile);
}
if(ac.equals(“Save”))
{
int result=chooser.showSaveDialog(this);
if(result!=JFileChooser.APPROVE_OPTION)
{
txtsave.setText(“Cancel was selected”);
return;
}
File selectedfile=chooser.getSelectedFile();
txtsave.setText(“” + selectedfile);
}
}
private JTextField txtopen,txtsave;
private JFileChooser chooser=new JFileChooser();
public JFileChooserDemo()
{
chooser.removeChoosableFileFilter( chooser.getAcceptAllFileFilter());
chooser.addChoosableFileFilter(new PngFilter());
chooser.addChoosableFileFilter(new JpgFilter());
chooser.setFileView(new DemoView());
txtopen=new JTextField(50);
txtsave=new JTextField(50);
setLayout(new FlowLayout());
setSize(700,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JButton bttnopen=new JButton(“Open”);
JButton bttnsave=new JButton(“Save”);
add(new JLabel(“Open”));
add(txtopen);
add(new JLabel(“Save”));
add(txtsave);
add(bttnopen);
add(bttnsave);
bttnopen.addActionListener(this);
bttnsave.addActionListener(this);
}
public static void main(String[] args)
{
JFileChooserDemo f=new JFileChooserDemo();
f.setVisible(true);
}
}

jpg filter view png file filter view

 

 

 

Leave a Reply