How to set UI for all Components of a type in java swing?

haferblues picture haferblues · Aug 30, 2011 · Viewed 7.3k times · Source

I created my own MyScrollbarUI class to have a custom scrollbar look in my application. Now I have to do

scrollPane.getHorizontalScrollBar().setUI(new MyScrollbarUI());
scrollPane.getVerticalScrollBar().setUI(new MyScrollbarUI());

on any ScrollPane I use.

Is it somehow possible to tell Swing that it should use MyScrollbarUI on any scrollbar. Maybe via the UIManager?

Answer

JB Nizet picture JB Nizet · Aug 30, 2011
UIManager.put("ScrollBarUI", MyScrollbarUI.class.getName());

should do the trick.

You need to have a public static ComponentUI createUI(JComponent c) method in your UI class, returning an instance of your UI.