Click to See Complete Forum and Search --> : waiting for a mouse event


Bill McHardy
March 26th, 1999, 11:22 PM
This problem probably has a really simple solution but it has me stumped. I am basically trying to get the code in my main() method to wait (do nothing) until the user clicks on a certain JButton component.

What I tried was to put a small loop in the main() method that would exit when a flag variable was set by my MouseEventListener method like so:


In main() ->


while(!clicked) ; //do nothing


In Listener ->


public void mouseClicked(...)

{ clicked = true; }


Not only does this not work but it wastes a lot of CPU time doing nothing! Please tell me how to do this right!

Thanks ahead of time!

Robert
April 12th, 1999, 03:34 PM
Hi Bill

why do you want the main() method to wait ?
You can program anything you want to happen in the
mouseClicked() method !!
The term while(!clicked())wastes a lot of time, because it's the main thread which halts everything in a GUI Program. Never make endless while() do constructions in the main thread because windows first accepts Events after leaving any method (which is not one of a thread or runnable class)
If you want to wait for an Event (here mouseEvent) only wait in the run() method of a thread class.
With the method setPriority(int) you can give the thread a HIGH_PRIORITY, NORM_PRIORITY or MIN_PRIORITY.
I hope this will help you.
Ask if any questions.

Bye Robert.