• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do I set the background to the default?

 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
forgot to include the UIManager way
tf.setBackground(UIManager.getColor("TextField.background"));
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great, so I'm in a lose/lose situation.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic