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

我的未来我决定 提交于 2019-12-22 05:59:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!