Search code examples
javaswinglook-and-feel

Programmatically get String Keys for LookAndFeels


Firstly, I found this question, which I know sounds a lot like my question, but I do have a slightly different question. From my research, I have found that some L&F's have different String keys than others, and I want to know how to set each value of the L&F using UIManager.put(String key, Object value).

The problem is that some L&Fs require different keys than others, and I am looking for a way to equate each property of each L&F so that I may programmatically set each value. I found this, which says how to get a value from a key using UIManager.getColor("key"), but don't know how to get the keys.

I also found this, which uses UIManager.getLookAndFeelDefaults();, but when I print out the objects in it, it produces a load of gobbledygook that I can't really use to find the keys (maybe this is how to find the keys, if you can figure it out, kudos to you).

I put this question's code into my own code, since it provides some of the keys. Here is the code I used:

UIManager.put( "control", new Color( 128, 128, 128) );
  UIManager.put( "info", new Color(128,128,128) );
  UIManager.put( "nimbusBase", new Color( 18, 30, 49) );
  UIManager.put( "nimbusAlertYellow", new Color( 248, 187, 0) );
  UIManager.put( "nimbusDisabledText", new Color( 128, 128, 128) );
  UIManager.put( "nimbusFocus", new Color(115,164,209) );
  UIManager.put( "nimbusGreen", new Color(176,179,50) );
  UIManager.put( "nimbusInfoBlue", new Color( 66, 139, 221) );
  UIManager.put( "nimbusLightBackground", new Color( 18, 30, 49) );
  UIManager.put( "nimbusOrange", new Color(191,98,4) );
  UIManager.put( "nimbusRed", new Color(169,46,34) );
  UIManager.put( "nimbusSelectedText", new Color( 255, 255, 255) );
  UIManager.put( "nimbusSelectionBackground", new Color( 104, 93, 156) );
  UIManager.put( "text", new Color( 230, 230, 230) );

I like the Nimbus theme for my project, however, I want to be able to switch the L&F within the application and be able to switch between the similar keys for each different L&F. Like I said, some keys may be different between L&F's for the same value used in the L&F.


Solution

  • This code should print the keys of the currently used L&F:

    UIDefaults defaults = UIManager.getDefaults();
    Enumeration<Object> keys = defaults.keys();
    Collections.list(keys).stream().forEach(s -> System.out.println(s));