-3

I changed the color of my button. Now I want its original look and feel back. I want to change the color once to a color that i like. After that, I want the button to get its original look back.

How do I do that ?

Here is the code to change color -

JButton but = JButton("Press now to up vote"); // :)
but.setBackground(Color.orange); 
//code to remove this color and get the original look back ???
mKorbel
  • 109,525
  • 20
  • 134
  • 319
SuperStar
  • 495
  • 2
  • 9
  • 22

2 Answers2

4
Color oldColor = myButton.getBackground();
myButton.setBackground(Color.RED);
// ... do stuff
myButton.setBackground(oldColor);
martinez314
  • 12,162
  • 5
  • 36
  • 63
  • Thanks. Is there a way to find out the name/code of the old color ? – SuperStar Apr 04 '13 at 21:04
  • 1
    You can do toString() on the Color object. It will output the components, for example: `java.awt.Color[r=255,g=0,b=0]`. There are other options to get the components. See the [Color API](http://docs.oracle.com/javase/6/docs/api/index.html?java/awt/Color.html). – martinez314 Apr 04 '13 at 21:10
4

I want the button to get its original look back.

  • this code should be only button.setBackground(null);

Is there a way to find out the name/code of the old color

  • by default Colors has not a names, only safe Colors(blue, red, orange ....)

  • old color not JButton has arrays of Colors

  • UIManager returns javax.swing.plaf.ColorUIResource[r=xxx, g=xxx, b=xx], contains arrays of Colors and Insets

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319