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



Question:

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.


Solution:1

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


Solution:2

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


Solution:3

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
Previous
Next Post »