In my program, I ask the user to enter a number. If they enter 2, I want a JFileChooser to come up and allow them to choose a file. However, when I run the program and enter 2, the JFileChooser never comes up.
Also,I tried running the debugger and it said that main was suspended when I do my call to the openFile method on line 24. But, I'm not sure what I can do to fix that. So, any help would be greatly appreciated.
Here is my code:
public class AddressBook {
public static void main(String[] args) {
new AddressBook().interact();
}
/**Allows the user to interact with the address book program.*/
public void interact(){
Scanner scan = new Scanner(System.in);
TreeMap addressBook;
System.out.println("What would you like to do? Enter a number to choose.");
System.out.println("1. Create a new address book.");
System.out.println("2. Open an existing address book.");
int answer = scan.nextInt();
if (answer == 1) {
addressBook = new TreeMap<String, Person>();
help();
}
else if (answer == 2) {
openFile();
help();
}
}
/**
* Open a selected file using a JFileChooser.
*
* @return the file that was opened
*/
public File openFile() {
File file = null;
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Load which file?");
int result = chooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
}
return file;
}
/**Displays a help menu containing a complete list of commands the user can
* enter, along with details explaining each command.
*/
public void help(){
System.out.println("Enter a letter to interact with the address book.");
System.out.println("h -- display the help menu");
System.out.println("a -- add an entry to the address book");
System.out.println("l -- look up an entry in the address book");
System.out.println("e -- edit an entry in the address book");
System.out.println("d -- delete an entry from the address book");
System.out.println("s -- save the address book");
}
}

New Topic/Question
Reply



MultiQuote




|