aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How do I set the background to the default? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How do I set the background to the default?" Watch "How do I set the background to the default?" New topic
Author

How do I set the background to the default?

Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
I have some JComponents, mostly JTextFields, that I need to highlight when the user is in the field. I do this by changing the background to a color of their choosing. Problem comes in trying to set the background back to it's default when the user leaves that field. I started simply holding the original background in a variable and setting it back, but that's no good when the color it should be changes while it's highlighted (i.e. disabling changes the background color).

Isn't there some way to set a JTextField or JComponents in general back to their default background, whatever that should be at the current time? Or could I at least get the default background color from UIManager or some place and set it myself? I can't seem to find where the keys for UIManager are specified...
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
try this
tf.setBackground(SystemColor.text)

for the UI's here's a link to Rob Camick's ShowUIDefaults program
http://www.discoverteenergy.com/files/ShowUIDefaults.java
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
forgot to include the UIManager way
tf.setBackground(UIManager.getColor("TextField.background"));
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Thanks. I'm not sure what the program you provided is for, a brief glance looks like it shows different LAF, that doesn't really help me but thanks anyway. I knew I could use the UIManager that way, I just don't know where the valid keys are documented. I just ended up quickly printing them out to a console by getting the Enumeration. Now I can use TextField.background and TextField.disabledBackground.

I still want to know, however, if this is a good practice? Are those keys guaranteed to be there? What happens if they're not and I end up in a LAF or system where they don't return anything, I'm going to be stuck in the same position.

Insight, anyone?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I'm not sure what the program you provided is for
it shows the UI default keys and their values (some with samples)

> Are those keys guaranteed to be there?
I've seen too many things broken between 1.4 and 1.5 to have any certainty
about anything working, as expected, in 1.6+
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Great, so I'm in a lose/lose situation.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How do I set the background to the default?