问题
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
?
回答1:
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.
回答2:
Try putting your custom UI class as ScrollPaneUI
property of UIManager. By default it is javax.swing.plaf.metal.MetalScrollPaneUI
change it to your custom class.
回答3:
technically, it's as easy as to tell the UIManager which delegate to use (as @Harry Joy already mentioned), like
UIManager.put("ScrollBarUI", "fully-qualified-className-of-customUI")
This will effectively install the same delegate class for all LAFs which might result less than optimal visuals in all except the one you extended your custom ui-class from. Strictly, you need one custom class per LAF
回答4:
Make new ScrollPane component by extending JScrollPane and tell your custom ScrollPane to use MyScrollbarUI. Then modify all your code to use your custom ScrollPane instead of JScrollPane.
来源:https://stackoverflow.com/questions/7242049/how-to-set-ui-for-all-components-of-a-type-in-java-swing