tags:

views:

46

answers:

0

Hi,

I want to change the look of a JScrollBar.
I do this with overwriting/extending ScrollBarUI.
It´s no problem to change the outlook of the arrowbuttons by overwriting createIncreaseButton and createDecreaseButton. I change the width of the track by overwriting paintThumb and paintTrack Methods.

It looks now like <----o----> (a very thin trackline and an oval thumb/knob).
PROBLEM:
The knob can't move till the very end:
What it does look like:    <---o------>
What it should look like: <---------o>

I know this is because I made the oval not stretching (the original rectangle stretches with the width).
I'm totally clueless as were to change the computing of the thumb move so it can move until the end.

I would be very thankful for help.

Heres the code:

public class TestScrollBarMain extends JFrame {

public TestScrollBarMain() {
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    JPanel p = new JPanel();
    p.setPreferredSize(new Dimension(500, 500));
    JScrollPane s = new JScrollPane(p);
    MyScrollBar b = new MyScrollBar();
    s.setVerticalScrollBar(b);
    getContentPane().add(s);
    setSize(100, 100);
    setVisible(true);
}

public static void main(String[] args) {
    new TestScrollBarMain();
}

public class MyScrollBarUI extends BasicScrollBarUI {

    @Override
    protected void paintThumb(final Graphics g, final JComponent c, final Rectangle thumbBounds) {
        if (thumbBounds.isEmpty() || !this.scrollbar.isEnabled()) {
            return;
        }
        g.translate(thumbBounds.x, thumbBounds.y);
        g.setColor(this.thumbDarkShadowColor);
        g.drawOval(2, 0, 14, 14);
        g.setColor(this.thumbColor);
        g.fillOval(2, 0, 14, 14);
        g.setColor(this.thumbHighlightColor);
        g.setColor(this.thumbLightShadowColor);
        g.translate(-thumbBounds.x, -thumbBounds.y);
    }

    @Override
    protected void paintTrack(final Graphics g, final JComponent c, final Rectangle trackBounds) {
        g.setColor(Color.black);
        g.fillRect(trackBounds.width / 2, trackBounds.y, 3, trackBounds.height);
        if (this.trackHighlight == BasicScrollBarUI.DECREASE_HIGHLIGHT) {
            this.paintDecreaseHighlight(g);
        } else if (this.trackHighlight == BasicScrollBarUI.INCREASE_HIGHLIGHT) {
            this.paintIncreaseHighlight(g);
        }
    }
}

public class MyScrollBar extends JScrollBar {

    MyScrollBar() {
        super();
        setUI(new MyScrollBarUI());
    }
}

}