• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about the stop button? What Listeners have youi got on that?
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic