Swing/DnDExportTabbedPane のバックアップソース(No.25)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/DnDExportTabbedPane へ行く。
- 1 (2009-03-23 (月) 14:24:48)
- 2 (2009-03-23 (月) 15:42:01)
- 3 (2009-05-19 (火) 17:31:12)
- 4 (2010-01-16 (土) 01:48:13)
- 5 (2010-01-16 (土) 12:25:38)
- 6 (2010-02-23 (火) 18:00:10)
- 7 (2010-06-17 (木) 01:57:34)
- 8 (2010-06-17 (木) 14:31:14)
- 9 (2010-06-17 (木) 15:44:07)
- 10 (2010-06-18 (金) 19:05:42)
- 11 (2010-07-08 (木) 21:54:27)
- 12 (2010-07-22 (木) 18:40:09)
- 13 (2010-07-23 (金) 14:35:15)
- 14 (2010-09-29 (水) 16:35:44)
- 15 (2010-09-30 (木) 16:16:35)
- 16 (2010-12-20 (月) 22:02:00)
- 17 (2012-02-05 (日) 11:13:40)
- 18 (2012-02-10 (金) 16:36:54)
- 19 (2013-01-10 (木) 17:54:46)
- 20 (2013-09-15 (日) 00:20:42)
- 21 (2014-11-01 (土) 00:46:09)
- 22 (2014-12-02 (火) 01:45:48)
- 23 (2015-01-23 (金) 19:25:52)
- 24 (2015-12-15 (火) 17:04:56)
- 25 (2016-05-26 (木) 14:44:43)
- 26 (2016-06-01 (水) 18:48:21)
- 27 (2017-03-29 (水) 15:44:09)
- 28 (2017-04-04 (火) 14:17:08)
- 29 (2017-08-15 (火) 14:45:52)
- 30 (2017-11-02 (木) 15:34:40)
- 31 (2017-11-17 (金) 19:54:09)
- 32 (2018-02-24 (土) 19:51:30)
- 33 (2019-07-17 (水) 14:58:33)
- 34 (2019-10-23 (水) 21:27:36)
- 35 (2021-05-27 (木) 16:28:46)
- 36 (2022-08-20 (土) 22:15:25)
--- title: JTabbedPane間でタブのドラッグ&ドロップ移動 tags: [JTabbedPane, TransferHandler, DragAndDrop, GlassPane, Cursor] author: aterai pubdate: 2009-03-23T14:24:48+09:00 description: JTabbedPane間でタブのDrag&Dropによる移動を行います。 hreflang: href: http://java-swing-tips.blogspot.com/2010/02/tabtransferhandler.html lang: en --- * 概要 [#yae9971e] `JTabbedPane`間でタブの`Drag&Drop`による移動を行います。 #download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTLW06ZMXI/AAAAAAAAAXc/vzeXm4pwhVY/s800/DnDExportTabbedPane.png) * サンプルコード [#e06345a4] #code(link){{ class TabTransferHandler extends TransferHandler { private final DataFlavor localObjectFlavor; public TabTransferHandler() { System.out.println("TabTransferHandler"); localObjectFlavor = new ActivationDataFlavor( DnDTabbedPane.class, DataFlavor.javaJVMLocalObjectMimeType, "DnDTabbedPane"); } //private static DnDTabbedPane source; //private synchronized static void setComponent(JComponent comp) { // if (comp instanceof DnDTabbedPane) { // source = (DnDTabbedPane) comp; // } //} //@Override public void exportAsDrag(JComponent comp, InputEvent e, int action) { // super.exportAsDrag(comp, e, action); // setComponent(comp); //} private DnDTabbedPane source = null; @Override protected Transferable createTransferable(JComponent c) { System.out.println("createTransferable"); if (c instanceof DnDTabbedPane) source = (DnDTabbedPane) c; return new DataHandler(c, localObjectFlavor.getMimeType()); } @Override public boolean canImport(TransferSupport support) { //System.out.println("canImport"); if (!support.isDrop() || !support.isDataFlavorSupported(localObjectFlavor)) { return false; } support.setDropAction(TransferHandler.MOVE); DropLocation tdl = support.getDropLocation(); Point pt = tdl.getDropPoint(); DnDTabbedPane target = (DnDTabbedPane) support.getComponent(); target.autoScrollTest(pt); DnDTabbedPane.DropLocation dl = (DnDTabbedPane.DropLocation) target.dropLocationForPoint(pt); int idx = dl.getIndex(); boolean isDropable = false; if (target == source) { isDropable = target.getTabAreaBounds().contains(pt) && idx >= 0 && idx != target.dragTabIndex && idx != target.dragTabIndex + 1; } else { if (source != null && target != source.getComponentAt(source.dragTabIndex)) { isDropable = target.getTabAreaBounds().contains(pt) && idx >= 0; } } Component c = target.getRootPane().getGlassPane(); c.setCursor(isDropable?DragSource.DefaultMoveDrop:DragSource.DefaultMoveNoDrop); if (isDropable) { support.setShowDropLocation(true); dl.setDropable(true); target.setDropLocation(dl, null, true); return true; } else { support.setShowDropLocation(false); dl.setDropable(false); target.setDropLocation(dl, null, false); return false; } } private BufferedImage makeDragTabImage(DnDTabbedPane tabbedPane) { Rectangle rect = tabbedPane.getBoundsAt(tabbedPane.dragTabIndex); BufferedImage image = new BufferedImage( tabbedPane.getWidth(), tabbedPane.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics g = image.getGraphics(); tabbedPane.paint(g); g.dispose(); if (rect.x < 0) { rect.translate(-rect.x, 0); } if (rect.y < 0) { rect.translate(0, -rect.y); } if (rect.x + rect.width > image.getWidth()) { rect.width = image.getWidth() - rect.x; } if (rect.y + rect.height > image.getHeight()) { rect.height = image.getHeight() - rect.y; } return image.getSubimage(rect.x, rect.y, rect.width, rect.height); } private static GhostGlassPane glassPane; @Override public int getSourceActions(JComponent c) { System.out.println("getSourceActions"); DnDTabbedPane src = (DnDTabbedPane) c; if (glassPane == null) { c.getRootPane().setGlassPane(glassPane = new GhostGlassPane(src)); } if (src.dragTabIndex < 0) return TransferHandler.NONE; glassPane.setImage(makeDragTabImage(src)); source = src; //setDragImage(img); //java 1.7.0-ea-b84 glassPane.setVisible(true); return TransferHandler.MOVE; } @Override public boolean importData(TransferSupport support) { System.out.println("importData"); if (!canImport(support)) return false; DnDTabbedPane target = (DnDTabbedPane) support.getComponent(); DnDTabbedPane.DropLocation dl = target.getDropLocation(); try { DnDTabbedPane source = (DnDTabbedPane) support.getTransferable() .getTransferData(localObjectFlavor); int index = dl.getIndex(); //boolean insert = dl.isInsert(); if (target == source) { source.convertTab(source.dragTabIndex, index); } else { source.exportTab(source.dragTabIndex, target, index); } return true; } catch (UnsupportedFlavorException ufe) { ufe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } return false; } @Override protected void exportDone(JComponent c, Transferable data, int action) { System.out.println("exportDone"); DnDTabbedPane src = (DnDTabbedPane) c; c.getRootPane().getGlassPane().setVisible(false); src.setDropLocation(null, null, false); } } }} * 解説 [#o96b9e3f] 上記のサンプルでは、`JDK 6`で導入された、`TransferHandler.DropLocation`を継承する`DnDTabbedPane.DropLocation`などを作成して、`JTabbedPane`間でタブの移動ができるように設定しています。 - 注意点 -- 自身の子である`JTabbedPane`にタブを移動することはできない -- 子コンポーネントが`null`(例えば`addTab("Tab",null)`)のタブは移動不可 -- タブが選択不可(例えば`setEnabledAt(idx, false)`)の場合は移動不可 - バグ? -- %%子コンポーネントが`JTable`の場合、マウスカーソルが点滅する%% --- %%`Windows`環境のみ?%% -- 子コンポーネントが`JTextArea`などの場合、ドラッグ中のタブゴーストが表示できない --- %%`JTable`、`JTextArea`どちらも、`JScrollPane`が影響している?%% ---`Java 1.7.0-ea-b84`以上で、`TransferHandler#setDragImage(Image)`を使用するとちゃんと表示される --- `textArea.setTransferHandler(null);`とすれば、`1.6.0`でも正常に表示される。 -- `SCROLL_TAB_LAYOUT`の場合、タブゴーストにスクロールボタンが表示される場合がある #ref(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTLZe_UIkI/AAAAAAAAAXg/bCzrlm037N8/s800/DnDExportTabbedPane1.png) * 参考リンク [#y58f4e18] - [[JTabbedPaneのタブをドラッグ&ドロップ>Swing/DnDTabbedPane]] - [[JLayerを使ってJTabbedPaneのタブの挿入位置を描画する>Swing/DnDLayerTabbedPane]] * コメント [#gcad5394] #comment - タブのドラッグ中、`JTable`上などで`Cursor`が点滅するのを修正。 -- &user(aterai); &new{2009-05-19 (火) 17:32:38}; - 点滅の原因は? -- &user(Dad); &new{2010-01-16 (土) 01:48:13}; -- おそらく、[http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6700748 Cursor flickering during D&D when using CellRendererPane with validation]が原因。現在は、`canImport`メソッド内で、一々`GlassPane#setCursor(isDropable?DragSource.DefaultMoveDrop:DragSource.DefaultMoveNoDrop);`として回避中。 -- &user(aterai); &new{2010-01-16 (土) 12:25:38}; - `6u20`ぐらいから`Web Start`で、`java.security.AccessControlException: access denied (java.awt.AWTPermission accessClipboard)`? -- &user(aterai); &new{2010-06-17 (木) 01:57:34}; -- チュートリアルのデモ[http://docs.oracle.com/javase/tutorial/uiswing/dnd/dropmodedemo.html Demo - DropDemo (The Java™ Tutorials > Creating a GUI With JFC/Swing Drag and Drop and Data Transfer)]でも同様のエラーが発生するので多分`Java 1.6.0_??`のバグ。 -- &user(aterai); &new{2010-06-17 (木) 14:31:14}; -- `6u21`では、修正されている?: [http://download.java.net/jdk6/6u21/promoted/b05/changes/JDK6u21.b05.list.html 6945178 2-High Defect SecurityException upon drag-and-drop(Bug/RFE fixed in JDK 6u21 build)] -- &user(aterai); &new{2010-06-17 (木) 14:32:45}; -- 修正されたようです。[http://www.oracle.com/technetwork/java/javase/bugfixes6u21-156339.html Java SE 6 Update 21 Bug Fixes] -- &user(aterai); &new{2010-07-08 (木) 21:54:27}; - `getTabPlacement()==RIGHT`などの場合に、`DropTarget`の描画がおかしくなるのを修正。 -- &user(aterai); &new{2012-02-05 (日) 11:13:40}; #comment