Application Using JFileChooser in Java

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:
  1. 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);
  1. 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);
  1. showDialog(component owner, string): This method shows a dialog box with an approval button which is user-defined.
            Syntax
            int showDialog(component owner,string);
  1. getSelectedFile(): This method returns the file that is selected by the user.
            Syntax
            File getSelectedFile();
  1. setCurrentDirectory(File f): This method sets the current directory as specified.
            Syntax
            void setCurrentDirectory(File f);
  1. getCurrentDirectory(): This method of JFileChooser returns the current directory as selected by the user.
            Syntax
            File getCurrentDirectory();
  1.  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
  1. import java.awt.*;  
  2. import javax.swing.*;  
  3. import java.awt.event.*;  
  4. import java.io.*;  
  5. public class OpenMenu extends JFrame implements ActionListener   
  6. {  
  7.  JMenuBar mb;  
  8.  JMenu file;  
  9.  JMenuItem open;  
  10.  JTextArea ta;  
  11.  OpenMenu() {  
  12.   open = new JMenuItem("Open File");  
  13.   open.addActionListener(this);  
  14.   file = new JMenu("File");  
  15.   file.add(open);  
  16.   mb = new JMenuBar();  
  17.   mb.setBounds(0030020);  
  18.   mb.add(file);  
  19.   ta = new JTextArea(300300);  
  20.   ta.setBounds(020300300);  
  21.   add(mb);  
  22.   add(ta);  
  23.  }  
  24.  public void actionPerformed(ActionEvent e)   
  25.  {  
  26.   if (e.getSource() == open)   
  27.   {  
  28.    openFile();  
  29.   }  
  30.  }  
  31.  void openFile()   
  32.  {  
  33.   JFileChooser fc = new JFileChooser();  
  34.   int i = fc.showOpenDialog(this);  
  35.   if (i == JFileChooser.APPROVE_OPTION)   
  36.   {  
  37.    File f = fc.getSelectedFile();  
  38.    String filepath = f.getPath();  
  39.    displayContent(filepath);  
  40.   }  
  41.  }  
  42.  void displayContent(String fpath)   
  43.  {  
  44.   try   
  45.   {  
  46.    BufferedReader br = new BufferedReader(new FileReader(fpath));  
  47.    String s1 = "", s2 = "";  
  48.    while ((s1 = br.readLine()) != null)   
  49.    {  
  50.     s2 += s1 + "\n";  
  51.    }  
  52.    ta.setText(s2);  
  53.    br.close();  
  54.   }   
  55.   catch (Exception e)   
  56.   {  
  57.    e.printStackTrace();  
  58.   }  
  59.  }  
  60.  public static void main(String[] args)   
  61.  {  
  62.   OpenMenu om = new OpenMenu();  
  63.   om.setSize(300300);  
  64.   om.setLayout(null);  
  65.   om.setVisible(true);  
  66.   om.setDefaultCloseOperation(EXIT_ON_CLOSE);  
  67.  }  
  68. }  
Output
 
The following is the screenshot of the output.
 
fig7.1.jpg