tags:

views:

43

answers:

3

In Swing when using a JTree/JList/JTable selecting an item changes its background color. Is there any way to retrieve this color for the current look and feel when the component is not selected? Thank you.

A: 

getBackground() should return you the current color of any component

BCunningham
Thank you, but I need the color before it is selected.
Sandro
+3  A: 

You can use :

UIManager.getColor("Tree.selectionBackground")

Other useful values are:

UIManager.getColor("Tree.selectionForeground")
UIManager.getColor("Tree.textBackground")
UIManager.getColor("Tree.textForeground")

EDIT: The other solutions proposed work of course but the downside is that you need a component and it has to be selected before you can call the method or you'll get the non-selected background. This solution can be called even if you don't have a tree to begin with :)

Savvas Dalkitsis
Cool, this is probably it. Is there a list of keys that are available somewhere?
Sandro
I asked this question myself many times and i never remember what the answer was :) I think there is a list somewhere but i can't remember off the top of my head. A little googling will get you there though... :)
Savvas Dalkitsis
confirmed and working, thank you.
Sandro
@sandro Full list of keys is dependent on LAF in use. You can get them using the following code: `for (Map.Entry entry : UIManager.getLookAndFeelDefaults().entrySet()){System.out.println(entry.getKey()+" : "+entry.getValue()} `
Taisin
Oh awesome, thank you.
Sandro
Or, if you are interested in the keys of a particular look and feel, you can look directly at the source code of the look and feel class. e.g. the class WindowsLookAndFeel.
Avrom
+2  A: 

For a list of all the UIManager values see UIManager Defaults.

camickr