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
2010-06-28 14:11:14
Thank you, but I need the color before it is selected.
Sandro
2010-06-28 14:13:21
+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
2010-06-28 14:11:39
Cool, this is probably it. Is there a list of keys that are available somewhere?
Sandro
2010-06-28 14:14:23
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
2010-06-28 14:15:39
@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
2010-06-28 14:44:30
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
2010-06-28 17:26:57