简体   繁体   中英

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM