This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to change the background color of Buttons in a JPanel?

 
Farhad Rahmati
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys

I know how to change the background color for buttons.

but if you 50 button in a panel and you want all of them to have same color. it's really hard to write code for 50 button or 200 button a panel to chage thier color.

is there a way to change all with very simple code.


Jbutton1.setbackground(Color.red)
...........
Jbutton50.setBackground(color.red)

thanks
 
Stephan van Hulst
Bartender
Pie
Posts: 5893
63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using 50-200 separate identifiers in the first place?

Just use a JButton[], and add all your buttons to it. Then you can simply set the colour of all of them in a for loop.
 
Farhad Rahmati
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea I used ArrayList

thank you very much

 
Stephan van Hulst
Bartender
Pie
Posts: 5893
63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice work
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic