Search code examples
javaswinguser-interfaceuimanager

How can I change the UI of all components of a certain type?


How can I set the UI for all components in my application without having to reference each one of them using component.setUI(...); ?

For example, I have several custom JScrollbars throughout my program,
and when you switch to another theme I have to reset all the UI's like this: scrollPane.getVerticalScrollBar().setUI(new CustomScrollBarUI());

I would prefer to not have to look up each one seperately when changing themes. Is there a way to do this?


Solution

  • You can change the UIDefaults early in your program:

    UIDefaults uiDefaults = UIManager.getDefaults();
    uiDefaults.put("ScrollBarUI", new CustomScrollBarUI());
    

    See also Changing UI Default Settings in Java: The UIDefaults Class.