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 ");
}
}
}