Waiting for a button click.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sebouh
    New Member
    • Feb 2007
    • 77

    Waiting for a button click.

    Hi guys. I don't know how to implement this so i was hoping if any of you can help me.

    I have a code like this one:
    Code:
    public static void main (String args[])
    {
        //create frame and button
        ....
    
        frame.setVisible(true);
        frame.add(button);
    
        do {
             ....
            // wait for a button click
    
            //code to be executed after a button click
             .....
    
            }while (true);
    }
    So i want to do some stuff and wait for a button click to continue. I set up a mouse listener and add it to the button (although the listener does nothing). The problem is, how can i wait for a button click. If this were a non-GUI application, i'd use a scanner and wait for an input from the user, which automatically halts the program. I figured using sleep would hang the application.

    Any thoughts?

    thanks a lot.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Sebouh
    Hi guys. I don't know how to implement this so i was hoping if any of you can help me.

    I have a code like this one:
    Code:
    public static void main (String args[])
    {
        //create frame and button
        ....
    
        frame.setVisible(true);
        frame.add(button);
    
        do {
             ....
            // wait for a button click
    
            //code to be executed after a button click
             .....
    
            }while (true);
    }
    So i want to do some stuff and wait for a button click to continue. I set up a mouse listener and add it to the button (although the listener does nothing). The problem is, how can i wait for a button click. If this were a non-GUI application, i'd use a scanner and wait for an input from the user, which automatically halts the program. I figured using sleep would hang the application.

    Any thoughts?

    thanks a lot.
    You don't use a mouselistener for listening for button clicks. Use an ActionListener instead. The code for handling the button click goes into the actionPerformed method.

    Comment

    • Sebouh
      New Member
      • Feb 2007
      • 77

      #3
      Yes, you're right. I forgot about that.

      But still, how would i get the application to wait for the click. I thought of something like this:
      Code:
      ...
          boolean clicked = false;
          ...
          while (!clicked) {
              Thread.sleep(1000);
          }
      
          ...
      }
      
      actionPerformed() {
      clicked = true;
      }
      But this would hang the application every time it sleeps.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Sebouh
        Yes, you're right. I forgot about that.

        But still, how would i get the application to wait for the click. I thought of something like this:
        Code:
        ...
            boolean clicked = false;
            ...
            while (!clicked) {
                Thread.sleep(1000);
            }
        
            ...
        }
        
        actionPerformed() {
        clicked = true;
        }
        But this would hang the application every time it sleeps.
        Just don't do anything until the actionPerformed method is called.

        Comment

        • Sebouh
          New Member
          • Feb 2007
          • 77

          #5
          I'm not sure what you mean by do nothing. If the the code is:
          Code:
          x = 1 + 1;
          //wait for button click
          x = 1 + 2;
          how can i do nothing? I have to put something in between to keep the app waiting, don't i?

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by Sebouh
            I'm not sure what you mean by do nothing. If the the code is:
            Code:
            x = 1 + 1;
            //wait for button click
            x = 1 + 2;
            how can i do nothing? I have to put something in between to keep the app waiting, don't i?
            Code:
            x = 1 + 2
            goes into the actionPerformed method.

            Comment

            • Sebouh
              New Member
              • Feb 2007
              • 77

              #7
              Originally posted by r035198x
              Code:
              x = 1 + 2
              goes into the actionPerformed method.
              I understand, but my original code had a while loop. I can't divide the while loop and put part of it in actionPerformed (), can i?

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by Sebouh
                I understand, but my original code had a while loop. I can't divide the while loop and put part of it in actionPerformed (), can i?
                Like this?
                [CODE=java]while(true) {
                //some stuff
                if(clicked) {
                //do stuff
                }
                }[/CODE]
                and you set the clicked flag in actionPerformed

                P.S Careful that you don't run yourself into an infinite loop

                Comment

                • Sebouh
                  New Member
                  • Feb 2007
                  • 77

                  #9
                  I'm afraid i will run to an infinite loop. If the player takes his time to click the button, then i'll constantly loop over the other part.

                  I was hoping to some sort of a "wait" solution that halts the application. I mean, how does the scanner work when i request input? Does it loop too, or blocks the program? I'm thinking more in terms of block and sleep - click button - interrupt - wake up thing.

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by Sebouh
                    I'm afraid i will run to an infinite loop. If the player takes his time to click the button, then i'll constantly loop over the other part.

                    I was hoping to some sort of a "wait" solution that halts the application. I mean, how does the scanner work when i request input? Does it loop too, or blocks the program? I'm thinking more in terms of block and sleep - click button - interrupt - wake up thing.
                    Even then someone/thread has to keep checking to see if the button was clicked.
                    You ran into an infinite loop because you had not handled all the conditions and included a break point from the loop.
                    Perhaps if you post the full code you are using and explain what you want to achieve with it ...

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      @OP: just for a little experiment: comment out that entire while loop in your main
                      method, i.e. that method only creates the frame and makes it visible. Run it and
                      see what happens.

                      kind regards,

                      Jos

                      Comment

                      Working...