Getting and Setting the Selected File of a JFileChooser Dialog

When setting the selected file, the file chooser dialog automatically sets the current directory.
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 chooser.setSelectedFile(f); } catch (IOException e) { } // Show the dialog; wait until dialog is closed chooser.showOpenDialog(frame); // Get the currently selected file File curFile = chooser.getSelectedFile();

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.