This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Use stop button to stop moving (stop timers) on JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Use stop button to stop moving (stop timers) on JPanel" Watch "Use stop button to stop moving (stop timers) on JPanel" New topic
Author

Use stop button to stop moving (stop timers) on JPanel

maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134
I have made many timers on my JPanel causing moving of rectangle in different directions. When one timer ends, the second starts and so on. I controlled that starting of timers by following the coordinates of the elements that draw rectangle.

Now i have created a Stop button and I would like to stop the current timer when I click on it. I tried to write that in actionPerformed method but it only stops the first timer and ignores others; I wrote like this: first I wrote stopping of all timers, it didn't work, then I wrote, for example: if(SortPanel2.time1.isRunning()){SortPanel2.time1.stop();} to check is the timer running and to stop it then - I wrote this checking for all timers, but it doesn't work still. Any help...?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38062
    
  22
We need more details. How are you calling the Timer to stop it? You could have a Timer reference which you assign a Timer to whenever you start it, then call that reference for stopping.
maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134
I made it like this:

I initialized the variable: y2=80. Then I wrote:



Code for time2:



I set delta=5.

Variable y22 is one of the coordinates of rectangle and timer causes it move vertically from 80 to 230. After that, rectangle goes horizontally and for that I use Timer time3, which I start and stop again by making the conditions with x2 coordinate of the rectangle. Condition is: if(y2>230 and for x2...).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38062
    
  22
What about the stop button? What Listeners have youi got on that?
maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134
I made it like this:



I added here code as I mentioned in my first thread (first to stop all timers and it didn't work, than I wrote: if(SortPanel2.time1.isRunning()){SortPanel2.time1.stop();} for all timers, but it didn't work neather).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Use stop button to stop moving (stop timers) on JPanel
 
Similar Threads
Help needed with java.util.Timer
Java EE timer service and periodical jobs
EJB Timers
How do I repaint only portions of the JPanel and/or Frame?
How to make an object strongly reachable?