• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

 
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...?
 
Marshal
Posts: 69843
278
  • 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
Marshal
Posts: 69843
278
  • 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).
 
bacon. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic