I'm writing a text based adventure game and I ran into a problem. I'm porting it over from terminal to JFrame GUI and I need it to wait for user input before proceeding. I set up a system where there is a boolean variable called buttonPressed which starts out as false. When the submit text button is pressed it changes it to true. There is a while loop that waits for the button to become true before it allows the program to continue, essentially pausing the program until the user submits input. The problem is this only works when I have inside the while loop a system.out.println line written in. Otherwise it doesn't continue after I click the button. What's the problem?
public class event implements ActionListener{
public void actionPerformed(ActionEvent e){
moves += 1;
movesLabel.setText("Moves: " + moves);
buttonPressed = true;
try{
input = (String)(textfield.getText());
textfield.setText("");
}catch(Exception ex){
textfield.setText("Error");
}
}
}
public static void main (String args[]) {
Main gui = new Main();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
gui.setSize(650, 350);
gui.setTitle("Sprinkles Text Adventure");
setInventory();
textArea.setText(textDisplay("Welcome Adventurer!"));
textArea.setText(textDisplay("What is thy name: "));
while(!buttonPressed){
// this only works when I have system.out.println("something") written in
}
buttonPressed = false;
textArea.setText(textDisplay(input));
if ("alice".equals(input)||"Alice".equals(input)){
textArea.setText(textDisplay("Stop toking the magical pipe, "));
textArea.setText(textDisplay("you aren't alice and this isn't wonderland"));
textArea.setText(textDisplay("Now down the rabbit hole you go!"));
}
else if ("l3ikezI".equals(input)||"l3ikezi".equals(input)){
System.out.println("Magic and Technology flows through your veins");
System.out.println("Welcome back, Master");
for(int j = 0; j<14; j++){
Chest[j]=1;
}
}
else{
System.out.println("Enter " + input + " and good luck!\n\n");
}