This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cycling through multiple colors for a button by pressing it multiple times.

 
Michael Payte
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two colored buttons. For each button, pressing it is supposed to change its color. They are supposed to cycle through six different colors, with the sixth button press cycling back to the original color. However, when I click either button, they don't change color. Please help, thank you!

EDIT: To clarify, I don't want the button to cycle through all of the colors with a single click; I want the buttons to change to the next color with a single click. I want the first click to change the button's color to blue, the second to change it to red, the third to yellow, the fourth to magenta, the fifth to pink, the sixth back to green, the seventh to blue again, etc.

 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want the button color to cycle within a single click?
The code seems to try doing so, the for loops do change the color, but so fast that you only see green in the end.
Also consider using the switch statement, that many if-else don't look good
 
Campbell Ritchie
Sheriff
Posts: 48448
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put the colours into an array. Make the index a field (possibly of the listener class).The whole thing will go horribly wrong if you push the key for the 2147483648th time
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit Ghorpade wrote:the for loops do change the color, but so fast that you only see green in the end.

Actually, since the loop is running on the EDT, the button can't be repainted until after the loop exits and the actionPerformed(...) method returns.

Michael, look into using a javax.swing.Timer to cycle through the colors.
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The whole thing will go horribly wrong if you push the key for the 2147483648th time

... unless the number of colors is a power of 2 (Yes I can count. 6 isn't.)
 
Campbell Ritchie
Sheriff
Posts: 48448
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it will still go horribly wrong if you have 4 or 8 colours. It will, of course, as Darryl has said, go wrong in phase with itself. With 6 colours it will go wrong out of phase with itself.
There is a fiddle you can use with the bitwise and operator which will stop it going horribly wrong. That depends on the number of colours being a power of 2.
 
Michael Payte
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I don't want the button to cycle through all of the colors with a single click; I want the buttons to change to the next color with a single click. I guess I wasn't too clear about that, sorry. I want the first click to change the button's color to blue, the second to change it to red, the third to yellow, the fourth to magenta, the fifth to pink, the sixth back to green, the seventh to blue again, etc.
 
Paul Clapham
Sheriff
Pie
Posts: 20771
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then the actionPerformed methods should call "leftButton.setBackground" (etc) exactly once, with the colour you want to change the background to.

The problem then is to figure out what that colour is. Something like what Campbell posted might work for you, or you might want to write some code which keeps track of what the current colour is and gets the next colour based on that.
 
Michael Payte
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone, I've figured it out!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic