Swing/HandScroll のバックアップ差分(No.22)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/HandScroll へ行く。
- 1 (2006-05-11 (木) 15:08:19)
- 2 (2007-05-24 (木) 19:23:47)
- 3 (2007-07-05 (木) 18:18:22)
- 4 (2008-01-25 (金) 16:26:13)
- 5 (2008-02-19 (火) 15:05:34)
- 6 (2008-10-01 (水) 17:24:00)
- 7 (2008-12-19 (金) 03:05:51)
- 8 (2009-01-20 (火) 11:55:43)
- 9 (2009-01-20 (火) 13:50:46)
- 10 (2009-04-24 (金) 20:56:58)
- 11 (2010-09-02 (木) 08:07:52)
- 12 (2011-05-31 (火) 03:17:00)
- 13 (2011-10-03 (月) 18:03:55)
- 14 (2011-10-04 (火) 16:19:06)
- 15 (2011-12-26 (月) 14:17:48)
- 16 (2013-03-15 (金) 16:58:30)
- 17 (2013-05-11 (土) 03:30:34)
- 18 (2013-05-13 (月) 20:08:18)
- 19 (2013-08-01 (木) 17:57:44)
- 20 (2014-04-08 (火) 19:21:11)
- 21 (2014-11-01 (土) 00:46:09)
- 22 (2014-11-29 (土) 01:52:41)
- 23 (2016-02-17 (水) 16:26:14)
- 24 (2016-09-02 (金) 13:29:50)
- 25 (2017-10-13 (金) 14:55:11)
- 26 (2018-02-24 (土) 19:51:30)
- 27 (2018-04-05 (木) 15:57:14)
- 28 (2018-10-16 (火) 13:28:40)
- 29 (2019-10-15 (火) 17:33:07)
- 30 (2021-05-14 (金) 19:24:07)
- 31 (2022-08-20 (土) 22:15:25)
- 32 (2022-09-12 (月) 18:11:59)
- 33 (2022-09-14 (水) 05:13:23)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- title: JScrollPaneのViewportをマウスで掴んでスクロール tags: [JScrollPane, JViewport, MouseListener, MouseMotionListener, JLabel] author: aterai pubdate: 2006-01-02 description: JScrollPaneの窓の中をマウスで掴んで画像をスクロールします。 --- * 概要 [#ef47828b] `JScrollPane`の窓の中をマウスで掴んで画像をスクロールします。 #download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTNqjajfcI/AAAAAAAAAbI/Km-h7tWdYOo/s800/HandScroll.png) * サンプルコード [#e92a8d9e] #code(link){{ class HandScrollListener extends MouseAdapter { private final Cursor defCursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR); private final Cursor hndCursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR); private final Point pp = new Point(); @Override public void mouseDragged(MouseEvent e) { JViewport vport = (JViewport) e.getComponent(); Point cp = e.getPoint(); Point vp = vport.getViewPosition(); //= SwingUtilities.convertPoint(vport, 0, 0, label); Point vp = vport.getViewPosition(); //= SwingUtilities.convertPoint(vport, 0, 0, label); vp.translate(pp.x - cp.x, pp.y - cp.y); //if (r1.isSelected()) { label.scrollRectToVisible(new Rectangle(vp, vport.getSize())); //} else { // vport.setViewPosition(vp); //} pp.setLocation(cp); } @Override public void mousePressed(MouseEvent e) { e.getComponent().setCursor(hndCursor); pp.setLocation(e.getPoint()); } @Override public void mouseReleased(MouseEvent e) { e.getComponent().setCursor(defCursor); } } }} * 解説 [#bf42cf21] `JViewport`の原点(左上)を %%`SwingUtilities.convertPoint`メソッドを使って中の`JLabel`(画像)の座標に変換し、これを基準座標としています。この座標を%% マウスの移動に応じて変更し、`JComponent#scrollRectToVisible`メソッドの引数として使用することで、覗き窓のスクロールを行っています。 ---- `JComponent#scrollRectToVisible(...)`ではなく、`JViewport#setViewPosition(Point)`を使用すると、内部コンポーネントの外側に移動することができます。 * 参考リンク [#b337b5bb] - [[JScrollPaneのオートスクロール>Swing/AutoScroll]] - [http://sozai-free.com/ 2000ピクセル以上のフリー写真素材集] -- 猫の写真を拝借しています。 - [http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6333318 Bug ID: 6333318 JViewport.scrollRectToVisible( Rectangle cr ) doesn't scroll if cr left or above] - [http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6333318 Bug ID: 6333318 JViewport.scrollRectToVisible( Rectangle cr ) doesn't scroll if cr left or above] - [[JScrollPaneでキネティックスクロール>Swing/KineticScrolling]] - [[JTreeの余白をドラッグしてスクロール>Swing/TreeDragScroll]] * コメント [#ubd1d4e0] #comment - つかんで移動ということですが、移動方向が逆の気がします。 -- &user(名無し); &new{2006-02-25 (土) 01:24:46}; -- ご指摘ありがとうございます。確かに逆ですね。画像を掴んでというより、スクロールバーを掴んでみたいな動きになってました。修正しておきます。 -- &user(aterai); &new{2006-02-25 (土) 03:33:50}; - `SwingUtilities.convertPoint`の代わりに、`vport.getViewPosition()`を使用するように変更。スクリーンショットの更新。 -- &user(aterai); &new{2009-01-19 (Mon) 16:58:27}; - `JDK 1.7.0`では、`JViewport#setViewPosition(Point)`を使って右下外部に移動できなくなっている。`Heavyweight`と`Lightweight`コンポーネントが混在しても問題ないようにするために、内部で`revalidate()`しているのが原因? このサンプルでは`Lightweight`コンポーネントしか使用しないので、`revalidate()`しないように対応。 -- &user(aterai); &new{2011-10-03 (月) 18:03:55}; - サンプルほしい -- &new{2013-05-11 (土) 03:30:34}; -- ページ上部にソースコードなどへのリンクがあるので、使ってみてください。上げ忘れてるとかメンテ中などでなければダウンロードできると思います。 -- &user(aterai); &new{2013-05-13 (月) 20:08:18}; #comment