aspose file tools*
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
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: 3649
    
  17

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

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

thank you very much

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Nice work
 
 
subject: how to change the background color of Buttons in a JPanel?