开发者

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

开发者 https://www.devze.com 2023-03-31 22:21 出处:网络
I created my own MyScrollbarUI class to have a custom scrollbar look in my application. Now I have to do

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(ne开发者_StackOverflow社区w 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?


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.


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消