简体   繁体   中英

Custom design JScollPane Java Swing

I need to design this scrollbar for all my scrollpanes :

在此输入图像描述

With Java Swing. I am using Netbeans IDE. What is the simplest solution ?

Thank you very much.

Regards

You can customize the look of a Swing component by setting a custom UI . In the case of a scroll pane's scroll bar, you do

scrollPane.getVerticalScrollBar().setUI(new MyScrollBarUI());

where MyScrollBarUI is derived from javax.swing.plaf.basic.BasicScrollBarUI . To do this for all scroll bars (not only in JScrollPane instances), call

UIManager.put("ScrollBarUI", "my.package.MyScrollBarUI");

before you create any Swing components.

In MyScrollBarUI , you override the following methods:

public class MyScrollBarUI extends BasicScrollBarUI {

    @Override
    protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {
        // your code
    }

    @Override
    protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
        // your code
    }
}

Your scroll bar is graphically very simple, so it should not be too hard to implement.

1) override JToolBar

2) most of Custom Look and Feel overrode that

Don't forget that if you plan to use the UIManager to override all scrollbars like this

UIManager.put("ScrollBarUI", "my.custom.SimpleScrollBarUI");

then your SimpleScrollBarUI class have the createUI method, ie: 有createUI方法,即:

public class SimpleScrollBarUI extends BasicScrollBarUI {

    public static ComponentUI createUI(JComponent c) {
        return new SimpleScrollBarUI();
    }

    //...

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM