Java Tips Weblog

  • Blog Stats

    • 2,570,888 hits
  • Categories

  • Archives

UIManager Defaults

Posted by Rob Camick on October 9, 2008

The UIManager contains information about the default properties of each Swing component. This information is stored in the UIDefaults class in the form of key/value pairs. Each Look and Feel will have its own set of default properties. The UIManager can be used to make changes to these properties.

At times you may want to know what the default value of a property is for a specific component. You can use code like this to get the default font for a JTextField:

Font font = UIManager.getFont("TextField.font");

Other times you may want to change the default value of a property for a specific component. You can use code like this to change the default font for all JTextFields:

UIManager.put("TextField.font", new FontUIResource( yourFontHere ));

Of course not all properties of a component can be controlled through the UIManager, so the question is – what properties are managed by the UIManager? Well, as mentioned earlier, all the information is contained in the UIDefaults class. The UIManagerDefaults program will simply display this information in a nicely formatted way.

Note that not all Look and Feels (LAF) use the same properties, so this type of solution can be very LAF dependent.

Get The Code

UIManagerDefaults.java

39 Responses to “UIManager Defaults”

  1. BoBear2681 said

    Also it’s important to keep in mind that the property keys defined in the UIManager can vary from one LookAndFeel to the next. Sun’s “standard” looks – Windows, Metal and Motif – all utilize pretty much the same values (or maybe *all* the same values?). But this isn’t a guarantee.

    Nimbus, for example, doesn’t define many of the properties those other looks do, and defines some of its own. This unfortunately makes it difficult to do heavy customization of components through their ComponentUI’s that works across all LookAndFeels. At least, that’s something I’m currently struggling with. :)

  2. Rob Camick said

    Good point to remember. I’ve only ever really looked at the Metal and Windows LAF’s. I find that any UI change is not easy when dealing with multiple LAF’s as it needs to be done for every LAF.

    So this solution is probably best when dealing with a single LAF.

  3. T.B.M said

    Hello camickr!

    I was searching for a utility like this. And your program is just perfect!! You have introduced so many features in this simple utility, Thankyou! I really liked the ‘sample’ feature.

    Though everything is perfect, but there are few problems which I noticed:

    1- When the Look and Feel is changed, then all the Window Decorations disappear, Window decorations appear only in Metal LF. This maybe because of this statement:
    JFrame.setDefaultLookAndFeelDecorated(true);
    Perhaps removing this statement is a good idea because it almost serves no useful purpose ; )

    2- When I double click any cell, it allows editing. This causes problems when a ‘sample’ cell is double clicked. e.g. if the cell was showing some Icon and I double click it, the icon disappears and text appears which is editable, but when I click outside(even without editing the original text), the Icon or any thing inside the cell disappears. So, I think making the table cells uneditable is a good idea. I don’t know much about swing (don’t know how to make cell uneditable) but you certainly do, so, you can make the suggested changes (if you have spare time).

    Thanks once again, and yes, I wanted to ask about property keys like ‘swing.boldMetal’. Are there other keys like that? If so, where can I find them? because your utility or UIDefaults (to be more specific) does not list it.

    Thanks!

  4. Rob Camick said

    Properties contained in the UIManager are not documented anywhere (that I know of), so if a property doesn’t display in my program I don’t know anything about them. The only way to find all references to the properties would be to search the source code for all “UIManager.get” statements.

  5. Thanks for your good and very helpful work

  6. Saul Mena said

    Hello, Rob. Just wanted to tell you this is a great app. Very useful indeed. Thanks a lot!

  7. Rob Camick said

    Thanks, this posting is actually the #1 visited posting and #1 code download.

  8. BigDaddyLoveHandles said

    Rock on, d00d!

    Suggestion: for some components, it would be helpful to tweak values then see the result on a prototype component. JTabbedPane, for example, can have 13 different Color properties, and it’s not obvious how they map to the component. The next step after discovering the keys is to write some code to manually see how the keys map. If your demo allowed for that it would be most excellent.

    • Rob Camick said

      Interesting suggesting. At first glance it seems rather involved so I doubt I’ll get around to it in the near future, but I’ll keep it in the back of my mind.

  9. Janus said

    Thank you so, so, so VERY much!
    I’ve had a problem with a JTree with a custom rendering component (to add check boxes) and the layout wasn’t pretty using the Windows UI: The rows of the nodes in the table overlapped slightly. Thanks to your program I discovered “Tree.rowHeight”, so after using UIManager.put(“Tree.rowHeight”, 21) the rows were of equal size in the default java UI and Windows UI.

    Regards,
    Janus

  10. delak said

    awesome tool. thank you so much.

  11. Do you know of a small utility that would let me set these defaults before I run a Swing program?

    • Rob Camick said

      All you need to do is use UIManager.put(…) for all the properties you want to change. This must be done before you create any of the Swing components that use these properties.

      • That works and I already use it.

        But I was hoping I could either use a -D in the command line, or modify a specific UIManager value (not look and feel) before starting to run my application.

  12. jduprez said

    It has been mentioned above, and it’s not clear if you plan to modify it: the cells are editable, but:
    1) this has no purpose for the keys
    2) a custom editor (e.g. JColorChooser for COlorUIResources) would be handier for the “value” or “sample” columns
    3) editing has no effect on my system (even on the “sample” column)

    I don’t know the dynamics of the UIDefaults caching, but it would be great if one could see the effect of editing the values on the component. You seem to explain though, that once the UI class has read a value, it will no more be reloaded.
    Whatever the obstacle, if leveraging the edited value is not possible, it would be better to make the cells uneditable.

    That being said, thank you for your continued help to Swing developers, and for this nice and handy tool.
    Best regards.

    • Rob Camick said

      Yes, it has already been suggested to show the effects of changing a property on a component. I still don’t know any easy way to do this. The only way to do it would be a brute force approach to create each component with a default model and data of some kind and I don’t plan on doing this for every component.

      Making he cell non-editable has also been suggested before so I guess I should do that sometime.

  13. Wim said

    Hi, I just want to tell you this tool is great. Thank you!

  14. It seems there is a bug in retaining the window decorations when setDefaultLookAndFeelDecorated(true) is called. This bug is discussed on usenet, on the thread.

    I only mention this because your UIDefaults code has become the ‘defacto standard’ code that people are playing with, to try and figure a work-around.

    • Rob Camick said

      Yes, this has been mentioned before. I wasn’t sure how to fix it.

      Using the ideas presented in the link you referenced I came up with a solution. In addition to the suggestions made, I added a check to determine if the LAF supports custom decorations. This will allow the Metal LAF to use its decorations and other LAF’s to use the standard Windows decorations.

      I haven’t tested the changes on any other platform.

      It adds a little flicker when you switch between LAF’s, but I can’t find a way around this. Let me know what you think.

  15. Andy Jung said

    Great tool. Thanks!

  16. Esmaeil Ashrafi said

    Thank you
    Very useful utility
    Best regards

  17. cpet said

    thanks you have helped a lot!

  18. Chaitanya said

    Thanks!
    This is VERY helpful.

  19. André Uhres said

    Just for info: when changing Look&Feel I get an IllegalStateException: “Buffers have not been created”. This seems to be a Java bug. I’m using JDK 1.6.0_24.

    Cheers,
    André

  20. Whired said

    Thanks a lot! Great program!

  21. Anonymous said

    thanks for sharing it’s great. But I am trying to change some insets but it gives me errors!

    for example what is wrong with the following line?
    UIManager.put(“TabbedPane.tabInsets”, javax.swing.plaf.InsetsUIResource[top=0,left=9,bottom=1,right=9]);

    thank you for help

  22. M said

    Once a property is set, is there a way to change that property, say at will? I know this can be done for laf in general using the SwingUtilities.updateComponentTreeUI(whatever); but that doesn’t seem to flush new changes… any thoughts on how that might be accomplished? I have also tried pulling the defaults and removing the property entry before re-adding it. No luck thee either. :/

    • Rob Camick said

      I believe the updateComponentTreeUI(…) method will only update properties that implement the UIResource interface. My example above shows how to use the FontUIResource(…), Don’t use “new Font(….)”. If that doesn’t work then you will need to send an SSCCE using the “Contact Us” page.

  23. I have developed something similar to this, maybe someone would want to check it out http://www.hopsof.com/java-components-1/tools/uimanagerdefaultslookup

  24. Srijan Acharya said

    Man, you are a Java God. Thanks ! You ended up (unknowingly though) helping me with my School Project

  25. Drachenbauer said

    It doesn´t contain ComboBox.border

    • Rob Camick said

      Then there is no value. Try using UIManager.get(…) for components like JTextField and JComboBox to see the different results. Not every component will have the same properties.

  26. Anonymous said

    That’s what I was looking for. Thank you so much!

Leave a comment