7 Replies - 59073 Views - Last Post: 23 September 2009 - 02:25 PM Rate Topic: -----

#1 Mila  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 34
  • View blog
  • Posts: 193
  • Joined: 28-October 06

JTextField background color

Posted 02 December 2008 - 05:53 PM

Is it possible to set the background color of a JTextField back to the default after changing it?

In my program, in order to let the user know there input was invalid, I change the background of the JTextField:
JTextField someField = new JTextField();
someField.setBackground(Color.RED);



Now I want to set it back to the default color, but I can't seem to find the right color. It's not Color.GREY or Color.LIGHT_GREY. It's not even SystemColor.control or SystemColor.window. Short of reinstantiating the JTextField, is there a way to revert to this default state?

Mila

Is This A Good Question/Topic? 0
  • +

Replies To: JTextField background color

#2 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: JTextField background color

Posted 02 December 2008 - 05:58 PM

What OS are you running? Cause i know for mac os x the default is Gray. It will be different for windows and linux. cheers
Was This Post Helpful? 0
  • +
  • -

#3 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: JTextField background color

Posted 02 December 2008 - 06:00 PM

Theoretically, you could just do this.

// originalColor is a class variable or something

// right after you create the text field.
originalColor = JTEXTFIELD.getBackground();

//--------- when you want to set it back
JTEXTFIELD.setBackground(originalColor);


Hope this helps! :D
Was This Post Helpful? 1
  • +
  • -

#4 Mila  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 34
  • View blog
  • Posts: 193
  • Joined: 28-October 06

Re: JTextField background color

Posted 02 December 2008 - 06:04 PM

View PostLocke37, on 2 Dec, 2008 - 06:00 PM, said:

Theoretically, you could just do this.

// originalColor is a class variable or something

// right after you create the text field.
originalColor = JTEXTFIELD.getBackground();

//--------- when you want to set it back
JTEXTFIELD.setBackground(originalColor);


Hope this helps! :D


Yes! That works!

Thanks so much. It was driving me mad, since none of the other colors were quite right.

@markhazlett9 -- I'm running Windows, and Color.GREY was way too dark.

Mila
Was This Post Helpful? 0
  • +
  • -

#5 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: JTextField background color

Posted 02 December 2008 - 06:05 PM

Glad I could help! :D
Was This Post Helpful? 0
  • +
  • -

#6 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,027
  • Joined: 11-December 07

Re: JTextField background color

Posted 02 December 2008 - 06:36 PM

Another option is:

someField.setBackground(new JTextField().getBackground());


Locke37's suggestion is probably more efficient. This is probably more maintainable (especially if you have lots of copntrols that change colour), yet it seems a bit hacky to me... and that really gets on my nerves. Grrrr...

This post has been edited by cfoley: 02 December 2008 - 06:38 PM

Was This Post Helpful? 0
  • +
  • -

#7 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,027
  • Joined: 11-December 07

Re: JTextField background color

Posted 02 December 2008 - 06:41 PM

I've just had another thought. You could set the backgrounds of every control to red and use the setOpaque() method to toggle the colours. That only works if you have two states though.
Was This Post Helpful? 0
  • +
  • -

#8 ninjav  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 23-September 09

Re: JTextField background color

Posted 23 September 2009 - 02:25 PM

Another way I found useful was to query the UIMananger for the default color for a component. This way Look-and-Feel independence is ensured.

private Color goodFieldColor = UIManager.getColor("TextField.background");



You can find all key->value pairs for all components using the following snippet of code:

		UIDefaults defaults = UIManager.getDefaults();
		Enumeration en = defaults.keys();
		while (en.hasMoreElements()) {
			Object key = en.nextElement();
			Object val = defaults.get(key);
			System.out.format("KEY[%s] VALUE[%s]\n",
					key.toString(), (null != val ? val.toString() : "(null)"));
		}



Hope this helps.
Was This Post Helpful? 1

Page 1 of 1