0

I'm a rather basic programmer who has been assigned to make a GUI program without any prior experience with creating a GUI. Using NetBeans, I managed to design what I feel the GUI should look like, and what some of the buttons should do when pressed, but the main program doesn't wait for the user's input before continuing. My question is, how do I make this program wait for input?

    public class UnoMain {

    public static void main(String args[]) {
        UnoGUI form = new UnoGUI(); // GUI class instance
        // NetBeans allowed me to design some dialog boxes alongside the main JFrame, so
        form.gameSetupDialog.setVisible(true); // This is how I'm trying to use a dialog box

        /* Right around here is the first part of the problem.
         * I don't know how to make the program wait for the dialog to complete.
         * It should wait for a submission by a button named playerCountButton.
         * After the dialog is complete it's supposed to hide too but it doesn't do that either. */

        Uno Game = new Uno(form.Players); // Game instance is started
        form.setVisible(true); // Main GUI made visible
        boolean beingPlayed = true; // Variable dictating if player still wishes to play.
        form.playerCountLabel.setText("Players: " + Game.Players.size()); // A GUI label reflects the number of players input by the user in the dialog box.

        while (beingPlayed) {
            if (!Game.getCompleted()) // While the game runs, two general functions are repeatedly called.
            {

                Player activePlayer = Game.Players.get(Game.getWhoseTurn());
                // There are CPU players, which do their thing automatically...
                Game.Turn(activePlayer);
                // And human players which require input before continuing.

                /* Second part of the problem:
                 * if activePlayer's strategy == manual/human
                 *   wait for GUI input from either a button named 
                 *   playButton or a button named passButton */

                Game.advanceTurn();
                // GUI updating code //

            }
        }
    }
}

I've spent about three days trying to figure out how to integrate my code and GUI, so I would be grateful if someone could show me how to make this one thing work. If you need any other information to help me, please ask.

EDIT: Basically, the professor assigned us to make a game of Uno with a GUI. There can be computer and human players, the numbers of which are determined by the user at the beginning of the game. I coded the entire thing console-based at first to get the core of the game to work, and have since tried to design a GUI; currently this GUI only displays information about the game while it's running, but I'm not sure how to allow the code to wait for and receive input from the GUI without the program charging on ahead. I've investigated other StackOverflow questions like this, this, this, or this, but I cannot comprehend how to apply the answers to my own code. If possible, I'd like an answer similar to the answers in the links (an answer with code I can examine and/or use). I apologize if I sound demanding or uneducated and confusing; I've been working diligently on this project for a couple weeks and it's now due tomorrow, and I've been stressing because I can't advance until I figure this out.

TL;DR - How do I get my main program to wait and listen for a button click event? Should I use modal dialog boxes, or is there some other way to do it? In either case, what code needs to be changed to do it?

Community
  • 1
  • 1
  • What do you mean by *"wait for input?"* – MadProgrammer Mar 10 '13 at 20:08
  • I want the program to stop and wait for the user to press specific buttons from the GUI before advancing (the areas which I want there to be a wait are indicated with block comments). As it is now, the code runs on through and throws an error when it gets to the while loop because the user didn't get to input what they needed, and I don't know how to fix it. – The Squishy Ditto Mar 10 '13 at 20:25
  • Technically, you can't. Your operating in a event driven environment, this means you have to respond to any type of event that might occur in what ever order they come in. You can use someone like a modal dialog, which will block until the dialog is closed, but this may not be what you really want – MadProgrammer Mar 10 '13 at 20:32
  • A modal dialog sounds like it might suit what I need for the first part of my problem, since the program is relying on the user to give input to set the game's parameters up. I just don't know how to use one in the context of this code. – The Squishy Ditto Mar 10 '13 at 20:39

1 Answers1

1

Unlike console based programming, that typically has a well defined execution path, GUI apps operate within a event driven environment. Events come in from the outside and you react to them. There are many types of events that might occur, but typically, we're interested in those generate by the user, via mouse clicks and keyboard input.

This changes the way an GUI application works.

For example, you will need to get rid of your while loop, as this is a very dangerous thing to do in a GUI environment, as it will typically "freeze" the application, making it look like your application has hung (in essence it has).

Instead, you would provide a serious of listeners on your UI controls that respond to user input and update some kind of model, that may effect other controls on your UI.

So, to try and answer your question, you kind of don't (wait for user input), the application already is, but you capture that input via listeners and act upon them as required.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I excluded some GUI updating lines of code from what I posted because I was trying to keep things clear by showing only what was relevant; the while loop updates the GUI's appearance each turn and automatically takes care of what the CPU players do, so it doesn't look frozen but it scrolls through things really fast (which I don't mind as long as it runs). However, I don't know how to make it wait and listen for a human player's actions on a human player's turn before continuing. – The Squishy Ditto Mar 10 '13 at 20:46
  • First off, if you are using Swing, using any kind of loop like this has the potential to block the event dispatching thread, this is a very dangerous thing to do. I suspect you've got around it because your not respecting the [initial thread constraints](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). Basically, when some event occurs (ie the user clicks a button), you need to update the game state, which would then allow you to determin if its the users turn of the games turn – MadProgrammer Mar 10 '13 at 20:58
  • I'd be highly curious as to why this would warrant a downvote? In what ways does it it not answer the OPs question? In what ways could it be improved? – MadProgrammer Aug 23 '17 at 05:08