• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JScrollPaneのオートスクロール
#navi(../)
*JScrollPaneのオートスクロール [#h85af000]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2004-11-22~
更新日:&lastmod;

#contents

**概要 [#d87718d8]
JScrollPane上のマウスポインタに応じてラベルをオートスクロールします。
JScrollPane上でのマウスドラッグに応じてラベルをオートスクロールします。

#screenshot

**サンプルコード [#nf7c2bf6]
#code{{
 final JViewport vport = scroll.getViewport();
 scroller = new javax.swing.Timer(5, new ActionListener() {
   public void actionPerformed(ActionEvent e) {
     Rectangle vr = vport.getViewRect();
     int w = vr.width;
     int h = vr.height;
     Point pt = SwingUtilities.convertPoint(vport,0,0,label);
     rect.setRect(pt.x+point.x, pt.y+point.y, w, h);
     label.scrollRectToVisible(rect);
   }
 });
 vport.addMouseMotionListener(new MouseMotionAdapter() {
   public void mouseMoved(MouseEvent e) {
     scroller.stop();
     Point pt = e.getPoint();
     point.setLocation(SCROLL*(pt.x-startPoint.x), SCROLL*(pt.y-startPoint.y));
     scroller.start();
     startPoint.setLocation(pt);
   }
 });
class DragMoverListener extends MouseInputAdapter implements ActionListener {
  private static final int SPEED = 2;
  private final Cursor dc;
  private final Cursor hc = new Cursor(Cursor.HAND_CURSOR);
  private final Rectangle rect = new Rectangle();
  private final javax.swing.Timer scroller;
  private final JComponent label;
  private final JViewport vport;
  private Point startPt = new Point();
  private Point move = new Point();

  public DragMoverListener(JViewport vport, JComponent comp) {
    this.vport = vport;
    this.label = comp;
    this.dc = label.getCursor();
    this.scroller = new javax.swing.Timer(5, this);
    vport.addMouseMotionListener(this);
    vport.addMouseListener(this);
  }
  public void actionPerformed(ActionEvent e) {
    Rectangle vr = vport.getViewRect();
    int w = vr.width;
    int h = vr.height;
    Point ptZero = SwingUtilities.convertPoint(vport,0,0,label);
    rect.setRect(ptZero.x-move.x, ptZero.y-move.y, w, h);
    label.scrollRectToVisible(rect);
  }
  public void mouseDragged(MouseEvent e) {
    scroller.stop();
    Point pt = e.getPoint();
    move.setLocation(SPEED*(pt.x-startPt.x), SPEED*(pt.y-startPt.y));
    startPt.setLocation(pt);
    scroller.start();
  }
  public void mousePressed(MouseEvent e) {
    label.setCursor(hc);
    startPt.setLocation(e.getPoint());
    scroller.stop();
  }
  public void mouseReleased(MouseEvent e) {
    label.setCursor(dc);
  }
  public void mouseExited(MouseEvent e) {
    label.setCursor(dc);
    move.setLocation(0, 0);
    scroller.stop();
  }
}
}}
-&jnlp;
-&jar;
-&zip;

**解説 [#i40d83e6]
上記のサンプルでは、JViewport内の画像ラベルがマウスポインタの移動方向と距離に応じて自動的にスクロールします。マウスをクリックすると一時停止します。
上記のサンプルでは、JViewport内の画像ラベルをマウスでドラッグすると、その移動方向に応じて自動的にスクロールします。マウスをクリックすると停止します。

javax.swing.Timerを使うことでスクロールの開始、継続、停止を行っています。

JTextPaneで文字列を挿入したときに、最後まで自動スクロールしたい場合は、[[JTextPaneで最終行に移動>Swing/CaretPosition]]を参考にしてください。

**参考リンク [#j6c969ff]
-[[JScrollPaneのViewPortをマウスで掴んでスクロール>Swing/HandScroll]]
-[[JTextPaneで最終行に移動>Swing/CaretPosition]]
-[[2000ピクセル以上のフリー写真素材集>http://sozai-free.com/]]

**コメント [#paa26bcc]
- 猫の手スクロール風の動作に変更しました。 -- [[terai]] &new{2007-05-24 (木) 19:16:16};

#comment