0

I am trying to write a program that will prompt the user to select a folder and will then allow the user to display all the contents within the folder. I've been searching the webs and found several examples of using JFileChooser, including this one here. When I try to use the example in the previous link in JGrasp, it runs without issue. However, when I try to run it in NetBeans, I get errors (The DemoJFileChooser class won't compile because a return type is required, and the line in Main says can't find symbol DemoJFileChooser). I'm not sure what the issue is with it, but I've adapted it the best I can below

The below program compiles and runs fine, but when I select option 1, the program just freezes for a second, and then re-displays the menu. I can't get it to display the file selection dialog. What am I doing wrong?

import java.util.Scanner;
import javax.swing.JFileChooser;

public class FileDirectoryProcessing {

JFileChooser chooser;
String choosertitle;

public static void main(String[] args) {

    SelectionMenu:
    while (true) {
        Scanner scannerIn = new Scanner(System.in);
        System.out.println("0 - Exit");
        System.out.println("1 - Select Directory");
        System.out.println("2 – List directory contents");
        System.out.println("Select Option");
        try{
        int userInput = Integer.parseInt(scannerIn.nextLine());
        switch (userInput) {
            case 0:
                System.out.println("Thank you");
                break SelectionMenu;
            case 1:
                System.out.println("Select a directory");

                new FileDirectoryProcessing().ChooseDirectory();
                break;
            case 2:
                System.out.println("Directory Contents");
                break;
            default:
                System.out.println("Please make a valid selection");
                break;
        }

        } catch (NumberFormatException e){
            System.out.println("Please make a valid selection");
        }           
    }      
}

private void ChooseDirectory(){
    chooser = new JFileChooser(); 
    chooser.setCurrentDirectory(new java.io.File("."));
    chooser.setDialogTitle(choosertitle);
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

    chooser.setAcceptAllFileFilterUsed(false);

    if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { 
      System.out.println(chooser.getCurrentDirectory() 
         +  "getCurrentDirectory(): ");
      System.out.println("getSelectedFile() : " 
        +  chooser.getSelectedFile());
      }
    else {
      System.out.println("No Selection ");
    }
  }
}
c0der
  • 18,467
  • 6
  • 33
  • 65
tbatch
  • 1,398
  • 10
  • 21

1 Answers1

0

The method showOpenDialog accepts a component, whereas this (instance of FileDirectoryProcessing) is not a component, hence the issue.

Provide parent component instance in the method. Or for now you can also pass chooser (instance of JFileChooser) or null as well.

So, both of the below options will work fine:

chooser.showOpenDialog(chooser)

or

chooser.showOpenDialog(null)
Aman Chhabra
  • 3,824
  • 1
  • 23
  • 39
  • Hi Aman, this worked great. My program is at least compiling now, but the filechooser pane isn't appearing. I played around for a moment, and found it when I minimized all my other windows. It is popping up in the back. I looked through the methods to see if I could set it in the front, but I didn't find anything. Any suggestions? – tbatch Jun 23 '18 at 04:24
  • @skidwiz That already has an answer here: https://stackoverflow.com/questions/5129294/bringing-jfilechooser-on-top-of-all-windows – Aman Chhabra Jun 23 '18 at 04:30