Introduction
This article explains the JFileChooser class of Java. This will define a way in which one can implement JFileChooser with a small application using it.
JFileChooser class
JFileChooser is a pre-defined class of the javax.swing package that shows a file chooser dialog box. JFileChooser is a component that provides access to the file system. A JFileChoose component is used whenever we need to access a file for some reason, it is also used when a user needs to search for a file to open.
Constructor of JFileChooser
JFileChooser(): Constructor creates a new JFileChooser dialog box that starts at the home directory of the user.
Methods of JFileChooser class
The JFileChooser class basically provides three methods that display the file chooser dialogs, as in the following:
- showOpenDialog(component owner): This method opens a dialog box that contains an approval button, in other words, "Open", that opens a file from the dialog.
Syntax
int showOpenDialog(component owner);
- showSaveDialog(component owner): This method is used when the user wants to save the specific file that he/she is using. This dialog box contains an approval button, in other words, "Save", to save the file.
Syntax
int showSaveDialog(component owner);
- showDialog(component owner, string): This method shows a dialog box with an approval button which is user-defined.
Syntax
int showDialog(component owner,string);
- getSelectedFile(): This method returns the file that is selected by the user.
Syntax
File getSelectedFile();
- setCurrentDirectory(File f): This method sets the current directory as specified.
Syntax
void setCurrentDirectory(File f);
- getCurrentDirectory(): This method of JFileChooser returns the current directory as selected by the user.
Syntax
File getCurrentDirectory();
- getName(File f): This method returns the name of the file as given by the file argument.
Syntax
String getName(File f);
This is a small application using the JFileChooser class, that demonstrates the features of this class.
Source code
- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*;
- import java.io.*;
- public class OpenMenu extends JFrame implements ActionListener
- {
- JMenuBar mb;
- JMenu file;
- JMenuItem open;
- JTextArea ta;
- OpenMenu() {
- open = new JMenuItem("Open File");
- open.addActionListener(this);
- file = new JMenu("File");
- file.add(open);
- mb = new JMenuBar();
- mb.setBounds(0, 0, 300, 20);
- mb.add(file);
- ta = new JTextArea(300, 300);
- ta.setBounds(0, 20, 300, 300);
- add(mb);
- add(ta);
- }
- public void actionPerformed(ActionEvent e)
- {
- if (e.getSource() == open)
- {
- openFile();
- }
- }
- void openFile()
- {
- JFileChooser fc = new JFileChooser();
- int i = fc.showOpenDialog(this);
- if (i == JFileChooser.APPROVE_OPTION)
- {
- File f = fc.getSelectedFile();
- String filepath = f.getPath();
- displayContent(filepath);
- }
- }
- void displayContent(String fpath)
- {
- try
- {
- BufferedReader br = new BufferedReader(new FileReader(fpath));
- String s1 = "", s2 = "";
- while ((s1 = br.readLine()) != null)
- {
- s2 += s1 + "\n";
- }
- ta.setText(s2);
- br.close();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- public static void main(String[] args)
- {
- OpenMenu om = new OpenMenu();
- om.setSize(300, 300);
- om.setLayout(null);
- om.setVisible(true);
- om.setDefaultCloseOperation(EXIT_ON_CLOSE);
- }
- }
Output
The following is the screenshot of the output.