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

327 views Asked by At

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?

1

There are 1 answers

0
trashgod On BEST ANSWER

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.