Swing/ResizableComponents のバックアップソース(No.10)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/ResizableComponents へ行く。
- 1 (2008-05-19 (月) 13:41:14)
- 2 (2011-05-28 (土) 01:33:39)
- 3 (2014-11-09 (日) 02:40:46)
- 4 (2015-02-26 (木) 13:58:00)
- 5 (2016-11-22 (火) 21:03:43)
- 6 (2017-11-26 (日) 23:48:30)
- 7 (2018-09-18 (火) 18:46:09)
- 8 (2019-02-21 (木) 14:38:03)
- 9 (2019-05-22 (水) 19:35:38)
- 10 (2019-05-23 (木) 15:52:44)
- 11 (2021-02-17 (水) 08:19:22)
- 12 (2022-10-07 (金) 15:00:53)
- 13 (2024-02-03 (土) 13:54:45)
--- category: swing folder: ResizableComponents title: JComponentをマウスで移動、リサイズ tags: [JLayeredPane, MouseListener, MouseMotionListener, JComponent] author: aterai pubdate: 2008-05-19T13:41:14+09:00 description: JLayeredPaneに、マウスで移動、リサイズ可能なコンポーネントを追加します。 image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTRw-M85QI/AAAAAAAAAhs/BFyVP2IYoak/s800/ResizableComponents.png --- * 概要 [#summary] `JLayeredPane`に、マウスで移動、リサイズ可能なコンポーネントを追加します。 #download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTRw-M85QI/AAAAAAAAAhs/BFyVP2IYoak/s800/ResizableComponents.png) * サンプルコード [#sourcecode] #code(link){{ @Override public void mouseDragged(MouseEvent e) { if (startPos == null || startingBounds == null) return; Point p = SwingUtilities.convertPoint(e.getComponent(), e.getX(), e.getY(), null); int deltaX = startPos.x - p.x; int deltaY = startPos.y - p.y; int newX = getX(); int newY = getY(); int newW = getWidth(); int newH = getHeight(); JComponent parent = (JComponent) getParent(); Rectangle parentBounds = parent.getBounds(); Dimension min = new Dimension(50, 50); Dimension max = new Dimension(500, 500); switch (cursor) { case Cursor.N_RESIZE_CURSOR: { if (startingBounds.height + deltaY < min.height) { deltaY = -(startingBounds.height - min.height); } else if (startingBounds.height + deltaY > max.height) { deltaY = max.height - startingBounds.height; } if (startingBounds.y - deltaY < 0) { deltaY = startingBounds.y; } newX = startingBounds.x; newY = startingBounds.y - deltaY; newW = startingBounds.width; newH = startingBounds.height + deltaY; break; } case Cursor.NE_RESIZE_CURSOR: { if (startingBounds.height + deltaY < min.height) { deltaY = -(startingBounds.height - min.height); // ... }} * 解説 [#explanation] 上記のサンプルでは、ツールバーやポップアップメニューから、移動、リサイズ可能な`JTable`や`JTree`を`JLayeredPane`(`JLayeredPane`のデフォルトレイアウトマネージャは`null`)に追加できます。 - リサイズボーダーの描画などは、[http://www.jroller.com/santhosh/entry/resizable_components Resizable Components - Santhosh Kumar's Weblog]を参考 - マウスのドラッグによる移動、リサイズの最大値、最小値の制限などは、`%JAVA_HOME%/src/javax/swing/plaf/basic/BasicInternalFrameUI.java`からの引用 ---- - `JDK1.5`では、`JLayeredPane#setComponentPopupMenu`を使う場合、以下のようにダミーのマウスリスナーなどを追加しておく必要がある #code{{ layer.setComponentPopupMenu(new MyPopupMenu()); layer.addMouseListener(new MouseAdapter() {}); }} ---- - `JDK1.6`では、背面にある`JTable`のヘッダが前面にロールオーバーする場合がある #ref(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTRzAZnaVI/AAAAAAAAAhw/t9TWz3YYv6U/s800/ResizableComponents1.png) * 参考リンク [#reference] - [http://www.jroller.com/santhosh/entry/resizable_components Resizable Components - Santhosh Kumar's Weblog] * コメント [#comment] #comment #comment