• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JButton onClick change background color

 
Kevin Behr
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Campbell Ritchie
Sheriff
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 885
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post the answer you got here to the other forums where you posted the question?
 
Kevin Behr
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Short and sweet.
Of course this is presuming all buttons want to use the same color sequence.
 
Kevin Behr
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot! This will go to good use. I will try it out now.
 
Kevin Behr
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So for each button, would I call MyColorButton();?

I tried that, and it says that it cannot find the method...
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, got it! Thanks!
 
Campbell Ritchie
Sheriff
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it.
Thanks for the pointers.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maneesh Godbole:
Got it.
Thanks for the pointers.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic