Getting and Setting the Current Directory of a JFileChooser Dialog - Java Swing

Java examples for Swing:JFileChooser

Description

Getting and Setting the Current Directory of a JFileChooser Dialog

Demo Code

import java.io.File;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class Main {
  public static void main(String[] args) {
    JFileChooser chooser = new JFileChooser();

    try {//from ww  w  .j  av  a2  s  .c  o  m
      File f = new File(new File(".").getCanonicalPath());

      chooser.setCurrentDirectory(f);
    } catch (IOException e) {
    }

    // The following method call sets the current directory to the home directory
    chooser.setCurrentDirectory(null);

    // Show the dialog; wait until dialog is closed
    chooser.showOpenDialog(new JFrame());

    File curDir = chooser.getCurrentDirectory();  
  }
}

Related Tutorials