TITLE:JScrollPaneのオートスクロール

JScrollPaneのオートスクロール

編集者:Terai Atsuhiro~

作成日:2004-11-22
更新日:2023-07-15 (土) 22:14:03
  • category: swing folder: AutoScroll title: JScrollPaneのオートスクロール tags: [JScrollPane, JViewport, JScrollPane, DragAndDrop, Timer, MouseListener, MouseMotionListener] author: aterai pubdate: 2004-11-22T00:57:31+09:00 description: JScrollPane上でのマウスドラッグに応じてラベルをオートスクロールします。 image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTH2GCzRoI/AAAAAAAAAR0/FR7seILhmaM/s800/AutoScroll.png hreflang:
       href: https://java-swing-tips.blogspot.com/2008/06/mouse-drag-auto-scrolling.html
       lang: en

概要

JScrollPane上でのマウスドラッグに応じてラベルをオートスクロールします。

概要

JScrollPane上でのマウスドラッグに応じてラベルをオートスクロールします。

#screenshot

サンプルコード

#spanend
#spandel
class DragMoverListener extends MouseInputAdapter implements ActionListener {
#spanend
  private static final int SPEED = 2;
#spanadd
* サンプルコード [#sourcecode]
#spanend
#spanadd
#code(link){{
#spanend
#spanadd
class ViewportDragScrollListener extends MouseAdapter
#spanend
                                 implements HierarchyListener {
  private static final int SPEED = 4;
  private static final int DELAY = 10;
  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 Cursor hc = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
  private final 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 Timer(DELAY, new ActionListener() {
      @Override public void actionPerformed(ActionEvent e) {
        Container c = SwingUtilities.getAncestorOfClass(
            JViewport.class, label);
        if (c instanceof JViewport.class) {
          JViewport vport = (JViewport) c;
          Point vp = vport.getViewPosition();
          vp.translate(move.x, move.y);
          label.scrollRectToVisible(
              new Rectangle(vp, vport.getSize()));
        }
      }
    });
  }
  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);
#spanadd

#spanend
  @Override public void hierarchyChanged(HierarchyEvent e) {
    JComponent c = (JComponent) e.getSource();
    if ((e.getChangeFlags() & HierarchyEvent.DISPLAYABILITY_CHANGED) != 0
        && !c.isDisplayable()) {
      scroller.stop();
    }
  }
  public void mouseDragged(MouseEvent e) {
    scroller.stop();
#spanadd

#spanend
  @Override public void mouseDragged(MouseEvent e) {
    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();
  }
  public void mousePressed(MouseEvent e) {
    label.setCursor(hc);
#spanadd

#spanend
  @Override public void mousePressed(MouseEvent e) {
    e.getComponent().setCursor(hc); // label.setCursor(hc);
    startPt.setLocation(e.getPoint());
    move.setLocation(0, 0);
    scroller.stop();
  }
  public void mouseReleased(MouseEvent e) {
    label.setCursor(dc);
#spanadd

#spanend
  @Override public void mouseReleased(MouseEvent e) {
    e.getComponent().setCursor(dc); // label.setCursor(dc);
    scroller.start();
  }
  public void mouseExited(MouseEvent e) {
    label.setCursor(dc);
#spanadd

#spanend
  @Override public void mouseExited(MouseEvent e) {
    e.getComponent().setCursor(dc); // label.setCursor(dc);
    move.setLocation(0, 0);
    scroller.stop();
  }
}
  • &jnlp;
  • &jar;
  • &zip;

解説

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

解説

上記のサンプルでは、JViewport内の画像ラベルをマウスでドラッグするとその移動方向に応じて自動的にスクロールします。 javax.swing.Timerを使うことでスクロールの開始、継続、停止を行っています。
  • javax.swing.Timerを使うことでスクロールの開始、継続、停止を実行
  • 自動スクロール中に画像をマウスでクリックすると移動を停止
JTextPaneで文字列を挿入したときに、最後まで自動スクロールしたい場合は、JTextPaneで最終行に移動を参考にしてください。

参考リンク

参考リンク

コメント