Tag Archives: JFileChooser

Determining the selected file in the JFileChooser


The JFileChooser is a Swing component to choose files from the FileSystem. After you chose a file (or Directory you need to get the selected file back in your code. This example shows how it’s done.

JFileChooser chooser = new JFileChooser();

// Set the current directory to the 
// application's current directory
try {
    // Create a File object containing the
    // canonical path of the
    // desired file
    File f = new File(new File("filename.txt").getCanonicalPath());

    // Set the selected file
} catch (IOException e) {

// Show the dialog; wait until dialog is closed

// Get the currently selected file
File curFile = chooser.getSelectedFile();