This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Cycling through multiple colors for a button by pressing it multiple times. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Cycling through multiple colors for a button by pressing it multiple times." Watch "Cycling through multiple colors for a button by pressing it multiple times." New topic
Author

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

Michael Payte
Greenhorn

Joined: Sep 14, 2012
Posts: 14
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

Joined: Jun 06, 2007
Posts: 2716
    
    6

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


SCJP, SCWCD.
|Asking Good Questions|
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37937
    
  22
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

Joined: May 03, 2008
Posts: 4523
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

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

Joined: Oct 13, 2005
Posts: 37937
    
  22
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

Joined: Sep 14, 2012
Posts: 14
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Sep 14, 2012
Posts: 14
Thanks everyone, I've figured it out!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cycling through multiple colors for a button by pressing it multiple times.
 
Similar Threads
background color of a jframe
converting a string color to an object color
Shuffling an Array (without Repetition...)
Setting color in a JComboBox
JColorChooser customization problem