Swing/AutoScroll のバックアップの現在との差分(No.25)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/AutoScroll へ行く。
- 1 (2004-11-22 (月) 01:00:20)
- 2 (2005-02-03 (木) 02:03:49)
- 3 (2005-02-04 (金) 09:39:40)
- 4 (2005-04-28 (木) 04:33:03)
- 5 (2005-05-31 (火) 07:39:18)
- 6 (2005-09-22 (木) 21:57:05)
- 7 (2005-10-06 (木) 10:43:59)
- 8 (2006-02-05 (日) 04:36:32)
- 9 (2006-02-27 (月) 15:26:18)
- 10 (2006-04-12 (水) 19:33:18)
- 11 (2007-05-24 (木) 19:15:50)
- 12 (2008-01-25 (金) 16:19:46)
- 13 (2010-12-12 (日) 23:21:50)
- 14 (2011-03-20 (日) 16:59:32)
- 15 (2011-12-22 (木) 18:36:14)
- 16 (2011-12-31 (土) 02:11:57)
- 17 (2013-04-14 (日) 00:29:20)
- 18 (2014-02-17 (月) 18:33:03)
- 19 (2014-04-08 (火) 19:17:40)
- 20 (2014-06-27 (金) 15:49:06)
- 21 (2014-10-31 (金) 01:48:55)
- 22 (2014-11-28 (金) 16:07:30)
- 23 (2015-02-17 (火) 22:26:26)
- 24 (2016-09-02 (金) 12:32:40)
- 25 (2017-10-12 (木) 13:36:31)
- 26 (2018-02-24 (土) 19:51:30)
- 27 (2019-04-04 (木) 14:28:34)
- 28 (2021-01-13 (水) 17:59:02)
- 29 (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: http://java-swing-tips.blogspot.com/2008/06/mouse-drag-auto-scrolling.html href: https://java-swing-tips.blogspot.com/2008/06/mouse-drag-auto-scrolling.html lang: en --- * 概要 [#summary] `JScrollPane`上でのマウスドラッグに応じてラベルをオートスクロールします。 #download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTH2GCzRoI/AAAAAAAAAR0/FR7seILhmaM/s800/AutoScroll.png) * サンプルコード [#sourcecode] #code(link){{ 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 Timer scroller; private final JComponent label; private Point startPt = new Point(); private Point move = new Point(); public ViewportDragScrollListener(JComponent comp) { this.label = comp; this.dc = comp.getCursor(); this.scroller = new Timer(DELAY, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Container c = SwingUtilities.getAncestorOfClass(JViewport.class, label); Container c = SwingUtilities.getAncestorOfClass( JViewport.class, label); if (c instanceof JViewport.class) { JViewport vport = (JViewport) c; Point vp = vport.getViewPosition(); //= SwingUtilities.convertPoint(vport, 0, 0, label); Point vp = vport.getViewPosition(); vp.translate(move.x, move.y); label.scrollRectToVisible(new Rectangle(vp, vport.getSize())); //vport.setViewPosition(vp); label.scrollRectToVisible( new Rectangle(vp, vport.getSize())); } } }); } @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) { JViewport vport = (JViewport) e.getSource(); Point pt = e.getPoint(); 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); } @Override public void mousePressed(MouseEvent e) { e.getComponent().setCursor(hc); //label.setCursor(hc); e.getComponent().setCursor(hc); // label.setCursor(hc); startPt.setLocation(e.getPoint()); move.setLocation(0, 0); scroller.stop(); } @Override public void mouseReleased(MouseEvent e) { e.getComponent().setCursor(dc); //label.setCursor(dc); e.getComponent().setCursor(dc); // label.setCursor(dc); scroller.start(); } @Override public void mouseExited(MouseEvent e) { e.getComponent().setCursor(dc); //label.setCursor(dc); e.getComponent().setCursor(dc); // label.setCursor(dc); move.setLocation(0, 0); scroller.stop(); } } }} * 解説 [#explanation] 上記のサンプルでは、`JViewport`内の画像ラベルをマウスでドラッグすると、その移動方向に応じて自動的にスクロールします。マウスをクリックすると停止します。 上記のサンプルでは、`JViewport`内の画像ラベルをマウスでドラッグするとその移動方向に応じて自動的にスクロールします。 `javax.swing.Timer`を使うことでスクロールの開始、継続、停止を行っています。 - `javax.swing.Timer`を使うことでスクロールの開始、継続、停止を実行 - 自動スクロール中に画像をマウスでクリックすると移動を停止 ---- `JTextPane`で文字列を挿入したときに、最後まで自動スクロールしたい場合は、[[JTextPaneで最終行に移動>Swing/CaretPosition]]を参考にしてください。 * 参考リンク [#reference] - [[JScrollPaneのViewportをマウスで掴んでスクロール>Swing/HandScroll]] - [[JTextPaneで最終行に移動>Swing/CaretPosition]] -- `JTextPane`で文字列を挿入したときに最後まで自動スクロールするサンプル - [http://sozai-free.com/ 2000ピクセル以上のフリー写真素材集] * コメント [#comment] #comment - 猫の手スクロール風の動作に変更しました。 -- &user(aterai); &new{2007-05-24 (木) 19:16:16}; - ドラッグ中は、[[JScrollPaneのViewportをマウスで掴んでスクロール>Swing/HandScroll]]と同じ動作をするように変更しました。 -- &user(aterai); &new{2011-12-22 (木) 18:38:02}; - ドラッグ中は、[[JScrollPaneのViewportをマウスで掴んでスクロール>Swing/HandScroll]]と同じ動作にるよう変更しました。 -- &user(aterai); &new{2011-12-22 (木) 18:38:02}; #comment