TITLE:ScrollBarの表示を変更

ScrollBarの表示を変更

編集者:Terai Atsuhiro
作成日:2006-06-26
更新日:2021-11-12 (金) 13:55:35

概要

JScrollBarのバー表示を変更します。

#screenshot

サンプルコード

scrollPane.getVerticalScrollBar().setUI(new WindowsScrollBarUI() {
  protected void paintThumb(Graphics g,
                            JComponent c, Rectangle thumbBounds) {
    super.paintThumb(g,c,thumbBounds);
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
    Color oc = null;
    Color ic = null;
    JScrollBar sb = (JScrollBar)c;
    if (!sb.isEnabled() || thumbBounds.width>thumbBounds.height) {
      return;
    }else if(isDragging) {
      oc = SystemColor.activeCaption.darker();
      ic = SystemColor.inactiveCaptionText.darker();
    }else if(isThumbRollover()) {
      oc = SystemColor.activeCaption.brighter();
      ic = SystemColor.inactiveCaptionText.brighter();
    }else{
      oc = SystemColor.activeCaption;
      ic = SystemColor.inactiveCaptionText;
    }
    paintCircle(g2,thumbBounds,6,oc);
    paintCircle(g2,thumbBounds,10,ic);
  }
  private void paintCircle(Graphics2D g2, Rectangle thumbBounds,
                           int w, Color color) {
    g2.setColor(color);
    int ww = thumbBounds.width-w;
    g2.fillOval(thumbBounds.x+w/2,
                thumbBounds.y+(thumbBounds.height-ww)/2,
                ww,ww);
  }
});
  • &jnlp;
  • &jar;
  • &zip;

解説

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

コメント