I use this myButton.setBackground(myColor)
to change the JButton
background color to my color, how to find it's original default background color so I can change it back? I know I can save its default background color before I change and use that, but I wonder if Java stores it somewhere so that maybe I can call something like: myButton.getClass.getDefaultBackground()
to get it back ?
Asked
Active
Viewed 4.5k times
8

stealthjong
- 10,858
- 13
- 45
- 84

Frank
- 30,590
- 58
- 161
- 244
7 Answers
14
btn.setBackground(new JButton().getBackground());
how about this... it will get the default color of button

Shiburagi
- 141
- 1
- 2
-
This works and probably has pretty negligible overhead. I like it. – sage88 Oct 09 '14 at 04:11
3
This might help:
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/SystemColor.html
Toolkit.getDesktopProperty(java.lang.String)
Toolkit.getDesktopProperty("control");
// control - The color rendered for the background of control panels and control objects, such as pushbuttons.

mschmidt42
- 1,065
- 1
- 9
- 12
1
It works both with:
button.setBackground(null);
and
button.setBackground(new JButton().getBackground());
(when you create a new JButton, its background color is initialized as a null color)
So, choose the one you consider to be the best for your project

UnknownCoder
- 11
- 1
0
Don't try to get background from Jframe or other elements to apply it on the button; if you already changed it do this:
ElementToStyle.setBackground(null);
-
Welcome to StackOverflow, I edited your answer to make clarify your answer (still your content) and remove the insults. It's not a huge deal but it makes this site better as a whole. Again, welcome to StackOverflow and thank you for taking time to share your knowledge. – Kevin Dec 05 '14 at 03:38
0
Color cbt= jButton6.getBackground();
String color_button=cbt.getRed()+","+cbt.getGreen()+","+cbt.getBlue();
if you wont get RGB color button try this code

Shinwar ismail
- 299
- 2
- 9