File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I set the background to the default?

 
Ken Blair
Ranch Hand
Posts: 1078
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
forgot to include the UIManager way
tf.setBackground(UIManager.getColor("TextField.background"));
 
Ken Blair
Ranch Hand
Posts: 1078
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 1078
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great, so I'm in a lose/lose situation.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic