Java JFileChooser set file selection mode to file and directory

Description

Java JFileChooser set file selection mode to file and directory

import java.io.IOException;

import javax.swing.JFileChooser;

public class Main {

  public static void main(String[] args) throws IOException {
 // configure dialog allowing selection of a file or directory
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    int result = fileChooser.showOpenDialog(null);

    // if user clicked Cancel button on dialog, return
    if (result == JFileChooser.CANCEL_OPTION)
      System.exit(1);//from w w w.  ja  va 2s . c  o m

    // return Path representing the selected file
    System.out.println(fileChooser.getSelectedFile().toPath()); 
  }
}



PreviousNext

Related