• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JButton onClick change background color

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, got it! Thanks!
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it.
Thanks for the pointers.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Maneesh Godbole:
Got it.
Thanks for the pointers.



 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic