5

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?

mmmmmm
  • 32,227
  • 27
  • 88
  • 117
haferblues
  • 2,155
  • 4
  • 26
  • 39

4 Answers4

15
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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
3

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.

Harry Joy
  • 58,650
  • 30
  • 162
  • 207
2

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

kleopatra
  • 51,061
  • 28
  • 99
  • 211
0

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.