Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 516
haferblues Avatar asked Aug 30 '11 10:08

haferblues


People also ask

What are the various GUI components in Swing?

We know that Swing is a GUI widget toolkit for Java. Every application has some basic interactive interface for the user. For example, a button, check-box, radio-button, text-field, etc. These together form the components in Swing.


2 Answers

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.

like image 197
JB Nizet Avatar answered Sep 22 '22 04:09

JB Nizet


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.

like image 20
Harry Joy Avatar answered Sep 20 '22 04:09

Harry Joy