0

I would like to wait for my gui (using swing) to be complete and for the user to click on the button he would like. With this code and this while (this.choice == -2) {}, my program get stucked at this point and doesn't answer anymore. The gui is not loading in fact

I would like to return the good number on PlayATurn() after the user take his choice. The only thing that I can't change are the header of PlayATurn() and the number to give back wich are 1,0,-1.

Any idea how to resolve this ?

Here's the code

public class Human extends Player { //Player can be also a bot, that's why Human extend player
//The 

private int choix = -2;

private void setChoix(int n) {
    this.choix = n;
}

public int getChoix() { return this.choix; }


public void myWindow()  {
    JFrame frame = new JFrame();

    frame.setTitle("My title);
    frame.setSize(400, 150);
    frame.setBackground(Color.white);
    frame.setResizable(false);

    //pan.setLayout(new BorderLayout());
    JPanel pan = new JPanel();
    pan.setBackground(Color.white);

    JLabel displayLabel = new JLabel ("Player 1 : It's your turn to play");

    JPanel buttonPanel = new JPanel();
    JButton buttonGoDown = new JButton("Go up");
    JButton buttonGoUP = new JButton("Go down");
    JButton buttonTake = new JButton("Take an item");

    buttonGoDown.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            frame.setVisible(false);
            setChoice(1);
        }
    });
    buttonGoUP.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            frame.setVisible(false);
            setChoice(-1);
        }
    });
    buttonTake.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            frame.setVisible(false);
            setChoice(0);
        }
    });

    buttonPanel.add(buttonGoDown);
    buttonPanel.add(buttonGoUp);
    buttonPanel.add(buttonTake);

    pan.add(displayLabel,BorderLayout.NORTH);
    pan.add(buttonPanel, BorderLayout.SOUTH);
    frame.setContentPane(pan);
    //frame.setContentPane(pan);
    frame.setVisible(true);
}

@Override
public int playATurn() {
    myWindow();
    while (this.choice == -2) {} // THE PART THAT I WOULD LIKE TO REMOVE

    return choice;
}
Nicolas S.
  • 173
  • 2
  • 12
  • 1
    Get rid of the while loop. You're programming in an event-driven environment now, and shouldn't use linear console code, code that fights against the GUI library. Either respond to the button press, or if you want to totally block program flow until the user enters input, use a *modal* dialog such as a modal JDialog or JOptionPane. – Hovercraft Full Of Eels Jan 08 '18 at 17:31
  • 1
    For example in this program, simply respond to user input in the action listener. Base *how* the program responds by the *state* of the program, the variable which determines whose turn it is. – Hovercraft Full Of Eels Jan 08 '18 at 17:32
  • Also, please look at (and up-vote) MadProgrammer's answer to a [similar question](https://stackoverflow.com/a/15327117/522444). – Hovercraft Full Of Eels Jan 08 '18 at 17:32
  • Thanks for the answer, I will to do it via JDialog. Will come back to you after the try ! – Nicolas S. Jan 08 '18 at 17:37
  • Worked perfectly ! Thanks @HovercraftFullOfEels – Nicolas S. Jan 08 '18 at 18:21

0 Answers0