Java JFileChooser .getSelectedFile ()

Syntax

JFileChooser.getSelectedFile() has the following syntax.

public File getSelectedFile()

Example

In the following code shows how to use JFileChooser.getSelectedFile() method.


// w ww.j av  a  2s  .c  om

import java.io.File;

import javax.swing.JFileChooser;

public class Main {

  public static void main(String[] a) {
    JFileChooser fileChooser = new JFileChooser(".");
    int status = fileChooser.showOpenDialog(null);

    if (status == JFileChooser.APPROVE_OPTION) {
      File selectedFile = fileChooser.getSelectedFile();
      System.out.println(selectedFile.getParent());
      System.out.println(selectedFile.getName());
    } else if (status == JFileChooser.CANCEL_OPTION) {
      System.out.println("canceled");
    }
  }

}