Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • 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
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Blinking JButton  RSS feed

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Group!

I have a basic question. How can I successfully blink a JButtons backround color based on my code below? I tried using threads it goes off...will not come back on. I've looked into using Timer however that might be overkill for this. Code is below:

 
Sheriff
Posts: 23965
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not like that. You should never use threads in Swing applications if you plan for them to change what the GUI looks like -- unless you do it right. But that's a topic we don't need to get into because there's a class called javax.swing.Timer whose purpose is exactly what you're trying to do. Google for How to Use Swing Timers and you'll find the Oracle tutorial.
 
Scott Eric Catalano
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Paul Clapham:

Yes Swing timers...can't wrap my head around it...I need to sleep and work at it tomorrow. Its not exactly straight forward.
 
Marshal
Posts: 62216
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Scott Eric Catalano wrote:. . . ...I need to sleep and work at it tomorrow. Its not exactly straight forward.

Trying to write code when you are tired is a sure‑fire recipe for confusion and failure, but Swing Timers are by no means difficult to use. Start by looking in the Java™ Tutorials.
 
Master Rancher
Posts: 3036
106
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also create a class BlinkingJButton that extends JButton, and has a method 'startBlinking' with some parameters about the blinking, and a method 'stopBlinking'. Later on, you could make that class generic, for instance BlinkingJComponent<? extends JComponent> so tht you can use this class for other JComponents as well (say JLabels et cetera).
 
Scott Eric Catalano
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was able to solve by using the following code posted for others who may find it useful:

 
Campbell Ritchie
Marshal
Posts: 62216
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Afraid I can see problems with that code. Why have you got a static field, and why isn't it marked private? I would no longer use an anonymous class to write an action listener, but a λ.Note that on cannot be a local variable. I suggest you might be able to find a better name for that variable. I would have thought you would get an unpleasant effect with your components changing colour. A more object‑orieted way to do it woiuld be to follow the earlier suggestion of a colour‑changing subclass, then you can write this sort of thing:-The colours would be passed to the component's constructor.
 
Scott Eric Catalano
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell Ritchie:

I used a static because it through an error of "cannot find symbol and a cannot use non-static variable". Adding static fixed the error...no other errors are being thrown. Using "on" isn't throwing any errors and the colors are changing just fine. I will find out in a bit if the code fails when using in other sections of the master program.
 
Campbell Ritchie
Marshal
Posts: 62216
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Scott Eric Catalano wrote:@Campbell Ritchie:

I used a static because it through an error of "cannot find symbol and a cannot use non-static variable". Adding static fixed the error... . . . .

. . . and added a new error which would only become apparent if you have multiple instances of your program. Adding the keyword static to cure that confusing error message is a mistake. The correct solution is to move the code using the variable out of a static context.
 
Piet Souris
Master Rancher
Posts: 3036
106
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:(...)


The class that I had in mind would have a method such as:

and it would set up its internal timers for this,]. So no need for external timers. Of course there would be some overloaded versions of this method, that make use of the default values. Seems a nice exercise to write such a class.
 
Campbell Ritchie
Marshal
Posts: 62216
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both options would work; they are but slightly different. Both are more object‑oriented because they make the button do the work of changing colour. I think your suggestion is probably better because the button object does its own timing.
 
Piet Souris
Master Rancher
Posts: 3036
106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think so too. The advantage is also that when there are more blinking components, then you do not need to write an actionListener for each (if they all need their own blinking frequencies and colors).
 
You get good luck from rubbing the belly of a tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!