Site icon FahmidasClassroom

Use of Java JFileChooser

Swing2

JFilleChooser class can be used to open the dialog box for opening or saving file or folder. Different uses of this class have been shown in the tutorial. Create a Java project and add a java class named FileChooser to check the use of the JFileChooser.

Display Open Dialog box for selecting file:

Add the following required modules to use JFileChooser for opening the dialog box.

//Import the required modules
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;

Add the following content into the main() method to open dialogue box for opening file and folder.

//Create an object of JFileChooser
JFileChooser fc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
        
//Open the dialog box
int returnValue = fc.showOpenDialog(null);
        
//Check which button is selected
if (returnValue == JFileChooser.APPROVE_OPTION) {
    //Assign the selected file
    File selectedFile = fc.getSelectedFile();
    //Print the path in the console
    System.out.println(selectedFile.getAbsolutePath());
}

Display Open Dialog box for selecting folder:

Add the following required modules to use JFileChooser for opening dialog box.

//Import the required modules
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;

Add the following content into the main() method to open dialogue box for opening folder.

//Create an object of JFileChooser
JFileChooser fc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
//Set the title of the dialog
fc.setDialogTitle("Select directory to save the file: ");
//Set the selection mode
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        
//Display the dialog box
int returnValue = fc.showSaveDialog(null);
        
//Check which button is selected
if (returnValue == JFileChooser.APPROVE_OPTION) 
{
    //Check the selected item is directory or not
    if (fc.getSelectedFile().isDirectory()) 
    {
         //Print the selected directory in the console
	 System.out.println("You selected the directory: " + fc.getSelectedFile());
    }
}

Display Open Dialog box for selecting files and folders:

Add the following required modules to use JFileChooser for opening the dialog box.

//Import the required modules
import java.io.File;
import java.util.Arrays;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;

Add the following content into the main() method to open dialogue box for opening files and folders.

//Create an object of JFileChooser
JFileChooser fc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
//Set the title of the dialog
fc.setDialogTitle("Select multiple files and folder");
//Enable the multiple election 
fc.setMultiSelectionEnabled(true);
//Set the file selection mode
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

//Display the dialog box
int returnValue = fc.showOpenDialog(null);

//Check which button is selected
if (returnValue == JFileChooser.APPROVE_OPTION) 
{
    //Store the files and folders in an array
    File[] files = fc.getSelectedFiles();
    

    System.out.println("Selected folders are:\n");
    //Read the array and print the folder list
    Arrays.asList(files).forEach(x -> {
          //Check the value is folder or not
          if (x.isDirectory()) 
          {
               System.out.println(x.getName());
          }
    });		
            
    System.out.println("Selected files are\n");
    //Read the array and print the file list
    Arrays.asList(files).forEach(x -> {
         //Check the value is file or not
         if (x.isFile()) {
              System.out.println(x.getName());
         }
    });
}

Display Open Dialog box for selecting text files:

Add the following required modules to use JFileChooser for opening the dialog box.

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileSystemView;

Add the following content into the main() method to open dialogue box for opening files and folders.

//Create an object of JFileChooser
JFileChooser fc= new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
//Set the title of the dialog
fc.setDialogTitle("Select a text file");
//Disable the selection of all types of files
fc.setAcceptAllFileFilterUsed(false);
//Set the filtering of text files only
FileNameExtensionFilter filter = new FileNameExtensionFilter("Text Files", "txt");

fc.addChoosableFileFilter(filter);

//Display the dialog box
int returnValue = fc.showOpenDialog(null);
//Check which button is selected
if (returnValue == JFileChooser.APPROVE_OPTION) {
   System.out.println(fc.getSelectedFile().getPath());
}

Hope, you will be able to work with the file browsing dialog box after practicing the above examples.

Exit mobile version