I'm currently working on a small text game project as an exercise in Java. Just recently, I've come to a point where I am having difficulty with the logic behind the flow of the program.

It had started out as a console application with a very sequential series of events such as:

System.out.println("What is your name?");
Player.setName(console.nextLine());
System.out.println("What is your quest?");
//etc. etc.

Since then, I've decided to try and change my approach and start using a GUI so I can add neat little things like a health bar and visible stats. Plus, it's a bit of a challenge. However, I'm finding that actually navigating the menu and taking input is a problem. How would I take input from the JTextField (getText()), give the input to the menu for processing and output, and then answer the next question using the same JTextField? Any and all help is greatly appreciated! If you need any further clarifications, please ask.

String input = jTextField1.getText();

you can pass this input variable on as a parameter, and you can store it for later usage.
what exactly is your problem?

I'll try and explain it as best as I can. I don't have any of the code at hand so I apologize for the inconvenience. Well, what I had before was a Scanner which would use nextLine() and charAt(0) to get a character for menu selection (ex. Press 'c' to enter character creation). The Scanner's nextLine() method paused the execution of the program and took in user input before proceeding to a switch statement like this:

Scanner console = new Scanner(System.in);
String s = console.nextLine();
char c = s.charAt(0);
switch(c){
//code
}

Since I started using the GUI approach, I can no longer do so. If I instead use JTextField1.getText().charAt(0) and run the program, it will return a IndexOutOfBoundsException because the string is empty upon execution of the program. In other words, it will not wait for any text to be typed into the JTextField before running the getText() method. I realized this meant the input had to be reaction-based and, therefore, based on an ActionListener and actionPerformed method. I'll try and recreate a small representation of the previous menu to the best of my ability to demonstrate what I am talking about:

public class Menu
{
    public void mainMenu()
    {
        boolean finished = false;

        while(!finished)
        {
            displayMenuOptions(); //displays Menu header and options
            Scanner console = new Scanner(System.in);
            String s = console.nextLine();
            char c = s.charAt(0);

            switch(c)
            {
                case 'c':
                    createCharacter(); //Enter character creation process
                    break;
                case 'e':
                    finished = true;
                    break;
                default:
                    System.out.println("Invalid.");
                    break;
            }
        }
    System.exit(0);
    }

    public void createCharacter()
    {
        Scanner console = new Scanner(System.in);
        Player p = new Player();

        System.out.println("What is your name?");
        String name = console.nextLine();
        p.setName(name);
        //more code for character creation process
    }
}

There is another class called Viewer which handles the JFrame, JPanels, and other GUI components. Then, it creates a Menu and calls the mainMenu() method to display the options.

As you can see, the scanners can take in all the input and then assign them as necessary. So once the menu options come up, I can enter 'c' which would take me to the Character Creation process. The next step would be entering the name of my new character. However, now that this is GUI based, I'm not completely sure how I can achieve the same results with a JTextField and an ActionListener.

I hope this has made things a little clearer. As always, please let me know if further clarification is in order.

Edited 6 Years Ago by rpineres: added code formatting, corrected exception name

I realized this meant the input had to be reaction-based

this was also the case when you used the Scanner class, it waited for your input. so yes, you'll need to trigger an action

This article has been dead for over six months. Start a new discussion instead.