How to wait for a button click?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yarane
    New Member
    • May 2012
    • 1

    How to wait for a button click?

    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?
  • firexfighterx
    New Member
    • May 2012
    • 14

    #2
    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.

    This Swing Java Tutorial describes developing graphical user interfaces (GUIs) for applications and applets using Swing components

    Comment

    Working...