This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes how to change the background color of Buttons in a JPanel? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how to change the background color of Buttons in a JPanel?" Watch "how to change the background color of Buttons in a JPanel?" New topic
Author

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

Farhad Rahmati
Ranch Hand

Joined: Dec 16, 2010
Posts: 96
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

Joined: Sep 20, 2010
Posts: 3893
    
  18

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.


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Farhad Rahmati
Ranch Hand

Joined: Dec 16, 2010
Posts: 96
Yea I used ArrayList

thank you very much

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3893
    
  18

Nice work
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to change the background color of Buttons in a JPanel?
 
It's not a secret anymore!