0

In the code given below, taken from the JAVA API page for class JFileChooser:

JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
    "JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
   System.out.println("You chose to open this file: " +
        chooser.getSelectedFile().getName());
}

It is supposed to open a popup window to prompt for a file in the user's directory. May I know how we should initialize the 'parent' variable, or what values to assign to it so that this dialog window points to the user's directory?

user1802890
  • 33
  • 1
  • 4

2 Answers2

0

Just assign it a null value. If this is in your applet class or JFrame class you can also put this.

Willem
  • 376
  • 2
  • 8
0

To get the user's home directory you should use a system property:

System.out.println("User Home Path: "+System.getProperty("user.home"));
File parent = new File(System.getProperty("user.home")); // User home directory

In your case however the parent variable is of Component class. That means that you are supposed to pass it a JFrame or other AWT/Swing component which is the dialog's parent. Passing null here will create a dialog that is not related to any other GUI component.

alfonx
  • 6,936
  • 2
  • 49
  • 58