Tutorial :How can I get the background color of a selected element?


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.


You can use :


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 :)


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


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

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »