By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,806 Members | 1,380 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,806 IT Pros & Developers. It's quick & easy.

How to wait for a button click?

P: 1
I have this code where I want to get user input in the middle of a loop. So it will look something like this:

do{
//wait for user input
//code to be executed based on user input
}while(true)

If I were to use a Scanner, the program will wait for the user to enter the information, and then it will proceed with the code.
However, I would like to use a JButton instead, so user input is interpreted based on the button clicked.
How would I force the program to wait until the user has clicked a button?
May 4 '12 #1
Share this Question
Share on Google+
1 Reply


P: 14
A loop would not be the best solution in this situation. There are things in Java called event handlers. The event handler is like a "listener" (in your case the telling the program to wait part). The listener will wait for the user to press the button. The listener will act appropriately to what you want the button to do.

Here is a link you might want to check out.

http://docs.oracle.com/javase/tutori...nts/intro.html
May 14 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.