JFileChooser Component in Java

This is a tutorial in which we will going to create a program that has the JFileChooser Component using Java. The JFileChooser is used to let the user to choose or open a specific file. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of jFileChooserComponent.java. 2. Import the following packages:
  1. import java.io.*; //used for accessing the file class
  2. import javax.swing.*// used to access the JFrame and JFileChooser class
3. Initialize your variable in your Main, variable fileChooser for the JFileChooser class.
  1. JFileChooser fileChooser = new JFileChooser();
4. Instantiate JFileChooser class variable with its FileFilter method to filter files. We will use the accept method to accept any files that has a .gif, .png, and .jpg file extension. Then we will use the getDescription method to have the description of these file extension of images.
  1.     fileChooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
  2.        
  3.       public boolean accept(File f) {
  4.         return f.getName().toLowerCase().endsWith(".gif")|| f.getName().toLowerCase().endsWith(".png") || f.getName().toLowerCase().endsWith(".jpg") || f.isDirectory();
  5.       }
  6.  
  7.       public String getDescription() {
  8.         return "Image Files";
  9.       }
  10.     });
Now, create again the FileFilter method to filter the text files with a .txt, .docx, and .rtf file extension.
  1.     fileChooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
  2.       public boolean accept(File f) {
  3.         return f.getName().toLowerCase().endsWith(".txt")|| f.getName().toLowerCase().endsWith(".docx") || f.getName().toLowerCase().endsWith(".rtf") || f.isDirectory();
  4.       }
  5.  
  6.       public String getDescription() {
  7.         return "Text Files";
  8.       }
  9.     });
5. To open the JFileChooser component in the JFrame, initialize a variable named var with a showOpenDialog instantiating a JFrame.
  1. int var = fileChooser.showOpenDialog(new JFrame());
If our variable var will be equal to the JFileChooser's APPROVE_OPTION, then we will get the file name of the selected file using the getSelectedFile().getName() method and will display the filename.
  1.     int var = fileChooser.showOpenDialog(new JFrame());
  2.     if (var == JFileChooser.APPROVE_OPTION) {
  3.       String name = fileChooser.getSelectedFile().getName();
  4.       System.out.println("File name: " + name);
  5.     }
Output: output Here's the full code of this tutorial:
  1. import java.io.*; //used for accessing the file class
  2. import javax.swing.*;// used to access the JFrame and JFileChooser class
  3.  
  4.  
  5. public class jFileChooserComponent {
  6.  
  7.   public static void main(String[] args) {
  8.     JFileChooser fileChooser = new JFileChooser();
  9.  
  10.     fileChooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
  11.        
  12.       public boolean accept(File f) {
  13.         return f.getName().toLowerCase().endsWith(".gif")|| f.getName().toLowerCase().endsWith(".png") || f.getName().toLowerCase().endsWith(".jpg") || f.isDirectory();
  14.       }
  15.  
  16.       public String getDescription() {
  17.         return "Image Files";
  18.       }
  19.     });
  20.    
  21.     fileChooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
  22.       public boolean accept(File f) {
  23.         return f.getName().toLowerCase().endsWith(".txt")|| f.getName().toLowerCase().endsWith(".docx") || f.getName().toLowerCase().endsWith(".rtf") || f.isDirectory();
  24.       }
  25.  
  26.       public String getDescription() {
  27.         return "Text Files";
  28.       }
  29.     });
  30.  
  31.     int var = fileChooser.showOpenDialog(new JFrame());
  32.     if (var == JFileChooser.APPROVE_OPTION) {
  33.       String name = fileChooser.getSelectedFile().getName();
  34.       System.out.println("File name: " + name);
  35.     }
  36.   }
  37. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment