• 追加された行はこの色です。
  • 削除された行はこの色です。
---
title: ScrollBarの表示を変更
tags: [JScrollBar]
tags: [JScrollBar, JScrollPane]
author: aterai
pubdate: 2006-06-26T12:08:46+09:00
description: JScrollBarのバー表示を変更します。
description: JScrollPaneから縦JScrollBarを取得し、そのノブ上にアイコンを追加表示します。
---
* 概要 [#l19c9133]
`JScrollBar`のバー表示を変更します。
`JScrollPane`から縦`JScrollBar`を取得し、そのノブ上にアイコンを追加表示します。

#download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTOPy62F7I/AAAAAAAAAcE/M4J9GIXdfBY/s800/IconScrollBar.png)

* サンプルコード [#r85d249f]
#code(link){{
scrollPane.getVerticalScrollBar().setUI(new WindowsScrollBarUI() {
  @Override protected void paintThumb(Graphics g,
                            JComponent c, Rectangle thumbBounds) {
    super.paintThumb(g,c,thumbBounds);
    Graphics2D g2 = (Graphics2D) g;
class BasicIconScrollBarUI extends BasicScrollBarUI {
  @Override protected void paintThumb(
      Graphics g, JComponent c, Rectangle thumbBounds) {
    super.paintThumb(g, c, thumbBounds);
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
                        RenderingHints.VALUE_ANTIALIAS_ON);
    Color oc = null;
    Color ic = null;
    JScrollBar sb = (JScrollBar)c;
    if (!sb.isEnabled() || thumbBounds.width>thumbBounds.height) {
    JScrollBar sb = (JScrollBar) c;
    if (!sb.isEnabled() || thumbBounds.width > thumbBounds.height) {
      return;
    }else if(isDragging) {
    } else if (isDragging) {
      oc = SystemColor.activeCaption.darker();
      ic = SystemColor.inactiveCaptionText.darker();
    }else if(isThumbRollover()) {
    } else if (isThumbRollover()) {
      oc = SystemColor.activeCaption.brighter();
      ic = SystemColor.inactiveCaptionText.brighter();
    }else{
    } else {
      oc = SystemColor.activeCaption;
      ic = SystemColor.inactiveCaptionText;
    }
    paintCircle(g2,thumbBounds,6,oc);
    paintCircle(g2,thumbBounds,10,ic);
    paintCircle(g2, thumbBounds, 6, oc);
    paintCircle(g2, thumbBounds, 10, ic);
    g2.dispose();
  }
  private void paintCircle(Graphics2D g2, Rectangle thumbBounds,
                           int w, Color color) {
    g2.setPaint(color);
    int ww = thumbBounds.width-w;
    g2.fillOval(thumbBounds.x+w/2,
                thumbBounds.y+(thumbBounds.height-ww)/2,
                ww,ww);
  private void paintCircle(Graphics2D g2, Rectangle b, int w, Color c) {
    g2.setPaint(c);
    int ww = b.width - w;
    g2.fillOval(b.x + w / 2, b.y + (b.height - ww) / 2, ww, ww);
  }
});
}
}}

* 解説 [#p0c5ef9a]
上記のサンプルでは、`WindowsScrollBarUI`を取得して、垂直スクロールバーに円状のアイコンを表示しています。このため`Windows`では、ドラッグしている状態、カーソルがバー上にあってロールオーバーしている状態、通常の状態でこのアイコンの色が変わります。

スクロールバーの長さが足りない場合、アイコンの表示は行われません。

//* 参考リンク
* コメント [#c32fb82d]
#comment
#comment