469,356 Members | 2,016 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

Waiting for a button click.

78
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:
Expand|Select|Wrap|Line Numbers
  1. public static void main (String args[])
  2. {
  3.     //create frame and button
  4.     ....
  5.  
  6.     frame.setVisible(true);
  7.     frame.add(button);
  8.  
  9.     do {
  10.          ....
  11.         // wait for a button click
  12.  
  13.         //code to be executed after a button click
  14.          .....
  15.  
  16.         }while (true);
  17. }
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.
Oct 24 '07 #1
10 16159
r035198x
13,262 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. public static void main (String args[])
  2. {
  3.     //create frame and button
  4.     ....
  5.  
  6.     frame.setVisible(true);
  7.     frame.add(button);
  8.  
  9.     do {
  10.          ....
  11.         // wait for a button click
  12.  
  13.         //code to be executed after a button click
  14.          .....
  15.  
  16.         }while (true);
  17. }
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.
Oct 24 '07 #2
Sebouh
78
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:
Expand|Select|Wrap|Line Numbers
  1. ...
  2.     boolean clicked = false;
  3.     ...
  4.     while (!clicked) {
  5.         Thread.sleep(1000);
  6.     }
  7.  
  8.     ...
  9. }
  10.  
  11. actionPerformed() {
  12. clicked = true;
  13. }
But this would hang the application every time it sleeps.
Oct 24 '07 #3
r035198x
13,262 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. ...
  2.     boolean clicked = false;
  3.     ...
  4.     while (!clicked) {
  5.         Thread.sleep(1000);
  6.     }
  7.  
  8.     ...
  9. }
  10.  
  11. actionPerformed() {
  12. clicked = true;
  13. }
But this would hang the application every time it sleeps.
Just don't do anything until the actionPerformed method is called.
Oct 24 '07 #4
Sebouh
78
I'm not sure what you mean by do nothing. If the the code is:
Expand|Select|Wrap|Line Numbers
  1. x = 1 + 1;
  2. //wait for button click
  3. x = 1 + 2;
how can i do nothing? I have to put something in between to keep the app waiting, don't i?
Oct 24 '07 #5
r035198x
13,262 8TB
I'm not sure what you mean by do nothing. If the the code is:
Expand|Select|Wrap|Line Numbers
  1. x = 1 + 1;
  2. //wait for button click
  3. x = 1 + 2;
how can i do nothing? I have to put something in between to keep the app waiting, don't i?
Expand|Select|Wrap|Line Numbers
  1. x = 1 + 2
goes into the actionPerformed method.
Oct 24 '07 #6
Sebouh
78
Expand|Select|Wrap|Line Numbers
  1. 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?
Oct 24 '07 #7
r035198x
13,262 8TB
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?
Expand|Select|Wrap|Line Numbers
  1. while(true) {
  2.     //some stuff
  3.     if(clicked) {
  4.          //do stuff
  5.     }
  6. }
and you set the clicked flag in actionPerformed

P.S Careful that you don't run yourself into an infinite loop
Oct 24 '07 #8
Sebouh
78
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.
Oct 24 '07 #9
r035198x
13,262 8TB
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 ...
Oct 24 '07 #10
JosAH
11,448 Expert 8TB
@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
Oct 24 '07 #11

Post your reply

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

Similar topics

12 posts views Thread by Raymond Lewallen | last post: by
4 posts views Thread by Jeff | last post: by
2 posts views Thread by D | last post: by
9 posts views Thread by Jonathan Wood | last post: by
1 post views Thread by Marine | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.