• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JScrollPaneのオートスクロール
#navi(../)
RIGHT:Posted by [[aterai]] at 2004-11-22
*JScrollPaneのオートスクロール [#h85af000]
JScrollPane上でのマウスドラッグに応じてラベルをオートスクロールします。

-&jnlp;
-&jar;
-&zip;

//#screenshot
#ref(http://lh4.ggpht.com/_9Z4BYR88imo/TQTH2GCzRoI/AAAAAAAAAR0/FR7seILhmaM/s800/AutoScroll.png)

**サンプルコード [#nf7c2bf6]
#code{{
class DragMoverListener extends MouseInputAdapter implements ActionListener {
  private static final int SPEED = 2;
class ViewportDragScrollListener extends MouseAdapter
                                 implements HierarchyListener {
  private static final int SPEED = 4;
  private static final int DELAY = 10;
  private final Cursor dc;
  private final Cursor hc = Cursor.getPredefinedCursor(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();
  private Point move    = new Point();

  public DragMoverListener(JViewport vport, JComponent comp) {
    this.vport = vport;
  public ViewportDragScrollListener(JComponent comp) {
    this.label = comp;
    this.dc = label.getCursor();
    this.scroller = new javax.swing.Timer(5, this);
    vport.addMouseMotionListener(this);
    vport.addMouseListener(this);
    this.dc = comp.getCursor();
    this.scroller = new javax.swing.Timer(DELAY, new ActionListener() {
      @Override public void actionPerformed(ActionEvent e) {
        JViewport vport = (JViewport)label.getParent();
        Point vp = vport.getViewPosition();
        vp.translate(move.x, move.y);
        label.scrollRectToVisible(new Rectangle(vp, vport.getSize()));
      }
    });
  }
  @Override 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);
  @Override public void hierarchyChanged(HierarchyEvent e) {
    JComponent c = (JComponent)e.getSource();
    if((e.getChangeFlags() & HierarchyEvent.DISPLAYABILITY_CHANGED)!=0
       && !c.isDisplayable()) {
      scroller.stop();
    }
  }
  @Override public void mouseDragged(MouseEvent e) {
    scroller.stop();
    JViewport vport = (JViewport)e.getSource();
    Point pt = e.getPoint();
    move.setLocation(SPEED*(pt.x-startPt.x), SPEED*(pt.y-startPt.y));
    int dx = startPt.x - pt.x;
    int dy = startPt.y - pt.y;
    Point vp = vport.getViewPosition();
    vp.translate(dx, dy);
    label.scrollRectToVisible(new Rectangle(vp, vport.getSize()));
    move.setLocation(SPEED*dx, SPEED*dy);
    startPt.setLocation(pt);
    scroller.start();
  }
  @Override public void mousePressed(MouseEvent e) {
    label.setCursor(hc);
    ((JComponent)e.getSource()).setCursor(hc); //label.setCursor(hc);
    startPt.setLocation(e.getPoint());
    move.setLocation(0, 0);
    scroller.stop();
  }
  @Override public void mouseReleased(MouseEvent e) {
    label.setCursor(dc);
    ((JComponent)e.getSource()).setCursor(dc); //label.setCursor(dc);
    scroller.start();
  }
  @Override public void mouseExited(MouseEvent e) {
    label.setCursor(dc);
    ((JComponent)e.getSource()).setCursor(dc); //label.setCursor(dc);
    move.setLocation(0, 0);
    scroller.stop();
  }
}
}}

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

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

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

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

**コメント [#paa26bcc]
- 猫の手スクロール風の動作に変更しました。 -- [[aterai]] &new{2007-05-24 (木) 19:16:16};
- ドラッグ中は、[[Swing/HandScroll]]と同じ動作をするように変更しました。 -- [[aterai]] &new{2011-12-22 (木) 18:38:02};

#comment