Results 1 to 4 of 4
- 04-21-2009, 09:32 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 2
- Rep Power
- 0
Breaking for-loops with listeners?
I have an action listener on a button that starts a for-loop. I would like to add a key listener that causes that for-loop to stop, but instead it waits for the for-loop to finish before it executes the command. How can I get this for loop to stop before it is finished iterating?
For example:
start, stop are Components.
start.addMouseListener(new MouseListener(){
method mouseClicked(){
execute some for-loop
}
......
stop.addKeyListener(new KeyListener(){
method keyPressed(){
stop for-loop from the 'start' component
}
.......
- 04-21-2009, 09:38 PM #2
You want to have a boolean that becomes true when your event takes place... Then in your for loop you want to put:
Java Code:if(myBoolean){ break; }Who Cares... As Long As It Works...
- 04-22-2009, 12:43 AM #3
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
The problem is, event processing is single threaded in swing and awt. You are starting your loop from an actionlistener, which means no event processing can take place until that call returns.
To do what you are wanting to do, you need to launch your for loop in a different thread.
- 04-22-2009, 03:38 AM #4
Member
- Join Date
- Apr 2009
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Help Updating Tree via Listeners
By Seiki in forum SWT / JFaceReplies: 1Last Post: 03-27-2009, 10:46 AM -
Seriously need help on my listeners!!
By themburu in forum Java AppletsReplies: 4Last Post: 05-26-2008, 10:41 AM -
Breaking down an integer
By Emily in forum New To JavaReplies: 1Last Post: 03-06-2008, 06:39 PM -
Breaking huge text into multiple parts.Please help..
By waNnY in forum New To JavaReplies: 2Last Post: 02-18-2008, 04:24 AM -
Breaking from nested switch
By javaplus in forum New To JavaReplies: 3Last Post: 02-02-2008, 08:28 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks