This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes JButton color Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JButton color" Watch "JButton color" New topic
Author

JButton color

John Zelek
Greenhorn

Joined: Nov 10, 2012
Posts: 2
I am trying to set up an interface with nine buttons, each with a different color. How do I set/change the color of each of the buttons. I am not trying to change the text color on the button. I am trying to change the color of the button itself. Seems like a simple task but not finding anything. I am trying to mimic the game Simon Says and I would like each button to be a different color, not just the border

JButton b1 = new JButton("1");
//b1.setForeground(Color.RED);
//b1.setBackground(Color.RED);
gamePanel.add(b1);
Russell Slighton
Greenhorn

Joined: Nov 18, 2012
Posts: 5
You could try to add a border then setBackgroundColor(), however, after adding a border, the button will not darken when clicked and will also looses several graphical functions. (But not functional functions)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JButton color