4 Replies - 1825 Views - Last Post: 12 June 2013 - 04:50 PM Rate Topic: -----

#1 unknown500   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 30
  • Joined: 11-June 12

Opening a File From the Console

Posted 12 June 2013 - 03:53 PM

Hi, everyone. I am having a problem opening a file from the console in Java.

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



















Is This A Good Question/Topic? 0
  • +

Replies To: Opening a File From the Console

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5612
  • View blog
  • Posts: 14,686
  • Joined: 18-April 07

Re: Opening a File From the Console

Posted 12 June 2013 - 04:00 PM

I ran this in both Eclipse and on the command line and the JFileChooser comes up just fine. Are you getting any errors and did you make sure to include javax.swing.* as an import?

:)
Was This Post Helpful? 1
  • +
  • -

#3 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Opening a File From the Console

Posted 12 June 2013 - 04:04 PM

Comes up fine here. Stop all running instances. Clean and rebuild
Was This Post Helpful? 1
  • +
  • -

#4 unknown500   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 30
  • Joined: 11-June 12

Re: Opening a File From the Console

Posted 12 June 2013 - 04:18 PM

Ok. Now, I feel like a complete idiot. It was working all along! I just couldn't see the JFileChooser because my IDE window was maximized. Wow... Well, thank you everybody anyway. I appreciate your help.
Was This Post Helpful? 0
  • +
  • -

#5 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2425
  • View blog
  • Posts: 5,068
  • Joined: 11-December 07

Re: Opening a File From the Console

Posted 12 June 2013 - 04:50 PM

A similar thing happened to me when I was starting Java. The console stayed on top of my JOptionPane and I spent hours trying to "debug" it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1