How to open one or more files with JFileChooser

JFileChooser is a very good way to create dialogs to open, save and view files and folders.

To bring up the JFileChooser dialog from the opening, just type these two lines of code:

//Create file chooser
JFileChooser fc = new JFileChooser();
//the response when a button is clicked
int return = fc.showOpenDialog(component_parent);

showOpenDialog opens the opening dialog. This method returns one of three possible values that mention which of the options is chosen by the user:
  • JFileChooser.CANCEL_OPTION: the user has unopened;
  • JFileChooser.APPROVE_OPTION: the user has validated the opening;
  • JFileChooser.ERROR_OPTION: an error has occurred.
This code brings up the dialog box, selects and loads the chosen file:

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class JFileChooserTest {
public static void main(String[] args) {
final JFrame window = new JFrame();
window.setSize(200,200);
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
JMenuBar jmb = new JMenuBar();
JMenu jm = new JMenu("Ficher");
JMenuItem Open = new JMenuItem("Open");
jm.add(Open);
jmb.add(jm);

window.add(jmb,BorderLayout.NORTH);

JFileChooser fc = new JFileChooser();

Open.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
int val_retour = fc.showOpenDialog(window);

if (val_retour == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//display file name
System.out.println("File name: "+file.getName()+"\n");
//display the absolute path of the file
System.out.println("Absolute path: "+file.getAbsolutePath()+"\n");
} else {
System.out.println("Opening is canceled\n");
}
}
});
}
}
Output:

How to open one or more files with JFileChooser

The terminal screen after clicking 'Open':

File name:  decTo Binaire.cpp

Filename: C:\Users\VAIO\Documents\CC++\decTo Binaire.cpp
To retrieve the chosen file, use the method getSelectedFile(). If the return value is JFileChooser.APPROVE_OPTION, the returned value is an instance of the class File.

The filename and absolute path are obtained with getName() and getAbsolutePath() respectively. If the user cancels the operation, a message is displayed indicating the cancellation.

JFileChooser allows you to select one or more files. To open them all, we have to modify the code. The changes are in the instance declaration File which becomes an array of selected files. We should also ask JFileChooser to give us the possibility to select several files using  setMultiSelectionEnabled().

final JFileChooser fc = new JFileChooser(); 
fc.setMultiSelectionEnabled(true)
Open.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
int val_retour = fc.showOpenDialog(window);

if (val_retour == JFileChooser.APPROVE_OPTION) {
File[] file = fc.getSelectedFiles();
//Loop that runs through the set of files
for(File f:file){
System.out.println("File name: "+f.getName()+"\n");
System.out.println("Absolute path: "+f.getAbsolutePath()+"\n");
}
} else {
System.out.println("Opening is canceled\n");
}
}
});
Runtime:

Filename: decTo Binaire.cpp
Absolute path: C:\Users\VAIO\Documents\CC++\decTo Binaire.cpp

Filename: decalage_tableau.cpp
Absolute path: C:\Users\VAIO\Documents\CC++\decalage_tableau.cpp

File name: determinant_matrice.cpp
Absolute path: C:\Users\VAIO\Documents\CC++\determinant_matrice.cpp

Filename: equation-1-degre.cpp
Absolute path: C:\Users\VAIO\Documents\CC++\equation-1-degre.cpp
References:
Creating JMenuBar Components: JMenuBar
Java Doc: showOpenDialog method