开发者

Custom JScrollbar Problem (change the knob/thumb)

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 tr开发者_StackOverflow中文版ackline 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());
    }
}

}


Include this on your MyScrollBarUI code:

  protected void setThumbBounds(int x, int y,int width,int height)
  {
      super.setThumbBounds(x, y, 14, 14);
  }
  protected Rectangle getThumbBounds()
  {
      return new Rectangle(super.getThumbBounds().x,super.getThumbBounds().y,14,14);
  }
  protected Dimension getMinimumThumbSize()
  {
      return new Dimension(14,14);
  }
  protected Dimension getMaximumThumbSize()
  {
      return new Dimension(14,14);
  }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜