The Student Room Group

Making a while loop wait for JButton ActionListener. JAVA.

Hi all,
Basically, I have a loop that is taking integer input from a Scanner, and is using this input to decide the behaviour of the program using conditionals. For example, pseudo style;


while (true)
{
//code to be run before if statements

if scanner == 0
//code for scenario 1

else if scanner == 1
//code for scenario 2

else if scanner == 2
//code for scenario 3

else
//code for all other int inputs.
}


What I need to do is replace this method of taking input with a scanner with JButtons. I have designed the GUI, and need some way of accomplishing the above. The GUI and ActionListeners are in a separate class. I was thinking, that the loop could be paused some how (threading anybody??) and wait for the JButtons to be pressed, and at the same time each button sets a 0,1, or 2 value to a variable that is then used for the if statements.

Thanks.
Reply 1
Why do you need a loop at all? Why not just call Scanner.next() when you need to within the event procedures for the appropriate JButtons?

Quick Reply

Latest

Trending

Trending