aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JButton onClick change background color Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JButton onClick change background color" Watch "JButton onClick change background color" New topic
Author

JButton onClick change background color

Kevin Behr
Greenhorn

Joined: Jun 29, 2006
Posts: 23
Hey guys,

Similar to my prior post...

I have the following code



What I need to do is, each time the Button is clicked, change the background color of the button (so first its red, then orange, then yellow, etc.)

So I need a way to go through the array on each click, and then repeat. But I can't seem to get my loops right...

Does anyone know a better way?
Sravan Kumar
Ranch Hand

Joined: Sep 11, 2005
Posts: 121
1. Am assuming you declare this variable 'i' as an instance variable of the class.

2. It is also better to declare the array 'colors' as an instance variable and initialize it once. You dont have to create the array inside actionPerformed() everytime.

3. The solution is to use an 'if' condition in actionPerformed, not a while.

I tested with the following code and it worked fine:



keep smilin :: sravan<br /><a href="http://sravanpens.blogspot.com" target="_blank" rel="nofollow">I scribble here</a>
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
A far better way to use the index is with the % operator.

. . . colors[index++ % colors.length] . . .

You can reduce the entire method toYou might have problems when you have clicked 2147483647 times with arithmetical overflow, however. Try index++ and ++index, see which works better. You can divide it into 3 statements if you prefer:

index++;
index %= colors.length;
myButton.setBackground(colors[index]);

And colors is identical for all instances, so it could beneficially be a static field.
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
Could you post the answer you got here to the other forums where you posted the question?
Kevin Behr
Greenhorn

Joined: Jun 29, 2006
Posts: 23
Thanks for all your help guys, I really am learning a great deal by it.

Campbell, I get what you are saying. I have 32 buttons, so I want to set up a method ColorChanger() where I will put my code, then can I just do this for each button...?


Is this possible since all my buttons have different names?
(gboard_row1_btn1.setBackground(colors[index])

Also, the only other thing I am unclear on is initializing the index. I have to initialize it to be 0. So each time the button is pressed, won't it re-initialize to 0?

Thanks!
[ July 09, 2008: Message edited by: Kevin Behr ]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

Short and sweet.
Of course this is presuming all buttons want to use the same color sequence.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Kevin Behr
Greenhorn

Joined: Jun 29, 2006
Posts: 23
Thanks alot! This will go to good use. I will try it out now.
Kevin Behr
Greenhorn

Joined: Jun 29, 2006
Posts: 23
So for each button, would I call MyColorButton();?

I tried that, and it says that it cannot find the method...
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

Originally posted by Kevin Behr:
So for each button, would I call MyColorButton();?

I tried that, and it says that it cannot find the method...


Oh Boy!
MyColorButton IS the button.
You mentioned using many (~32) buttons.

So you do something like this.


That is it. No need to do anything else, except google for "java inheritance"
[ July 09, 2008: Message edited by: Maneesh Godbole ]
Kevin Behr
Greenhorn

Joined: Jun 29, 2006
Posts: 23
Oh, got it! Thanks!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
Agree about subclassing JButton, but your method of getting the index for the colour List is very clunky. I still think the % method is better.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

Umm.. can you elaborate on "clunky" please? Is it because of the COLOR_LIST.get(index)?

I had thought:
The color array is static. So compile time memory allocation would help a bit in optimization.
No chance of overflow.
No pre/post incremental confusion (I personally still get confused in this sometimes
Of course could be optimized to if it is going to result in a few less CPU cycles.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
The most inefficient part is the linear search through your List for the index.

You set up index as an instance field, in which case it defaults to 0;
Then you can go through the List and get the colour:

. . . colourList.get(index);
. . .
index++;
index %= colourList.size();

If you confine the ++ operator to its own statement there is no risk of confusion about it, and all these operations are very rapid. In fact the rate-limiting step is clicking the mouse!
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

Got it.
Thanks for the pointers.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
Originally posted by Maneesh Godbole:
Got it.
Thanks for the pointers.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JButton onClick change background color
 
Similar Threads
AWT Event look for help
change JTable cell background color dynamically
Background Pattern for a Cell in JTable
Graphics - BufferedImage - ImageIO problem
Changing colors on my swing app