my dog learned polymorphism*
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 Murach's Java Servlets and JSP this week in the Servlets 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: 3573
    
  14

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: 3573
    
  14

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?
 
Similar Threads
panels and background
Pulling out hair
can't see the background color of a frame , why?
setting applet's background color
Trying to make a button but don't know where to start