---
category: swing
folder: InternalFrameDropTarget
title: JInternalFrame間でのドラッグ&ドロップによるJTableの行入れ替え
tags: [JInternalFrame, JDesktopPane, JTable, DragAndDrop]
author: aterai
pubdate: 2014-05-19T00:37:47+09:00
description: JInternalFrame間でJTableの行をドラッグ&ドロップを使って入れ替えます。
image: https://lh5.googleusercontent.com/-UP_I_iTgtpc/U3jPL_jhwuI/AAAAAAAACFg/olJ1Sf-P_lU/s800/InternalFrameDropTarget.png
---
* Summary [#summary]
`JInternalFrame`間で`JTable`の行をドラッグ&ドロップを使って入れ替えます。
#download(https://lh5.googleusercontent.com/-UP_I_iTgtpc/U3jPL_jhwuI/AAAAAAAACFg/olJ1Sf-P_lU/s800/InternalFrameDropTarget.png)
* Source Code Examples [#sourcecode]
#code(link){{
private boolean isDroppableTableIntersection(TransferSupport info) {
Component c = info.getComponent();
if (!(c instanceof JTable)) {
return false;
}
JTable target = (JTable) c;
if (!target.equals(source)) {
JDesktopPane dp = null;
Container cn = SwingUtilities.getAncestorOfClass(JDesktopPane.class, target);
if (cn instanceof JDesktopPane) {
dp = (JDesktopPane) cn;
}
JInternalFrame sf = getInternalFrame(source);
JInternalFrame tf = getInternalFrame(target);
if (sf == null || tf == null || dp.getIndexOf(tf) < dp.getIndexOf(sf)) {
return false;
}
Point pt = SwingUtilities.convertPoint(
target, info.getDropLocation().getDropPoint(), dp);
Rectangle rect = sf.getBounds().intersection(tf.getBounds());
if (rect.contains(pt)) {
return false;
}
// tf.moveToFront();
// tf.getParent().repaint();
}
return true;
}
}}
* Description [#explanation]
* Description [#description]
上記のサンプルでは、異なる`JInternalFrame`に配置した`JTable`の行をドラッグ&ドロップで入れ替え可能にしています。
- 使用する`TransferHandler`は[[JTableの行を別のJTableにドラッグして移動>Swing/DragRowsAnotherTable]]のものとほぼ同等
- 前面の`JInternalFrame`内にある`JTable`からドラッグを開始した場合その`JTableHeader`や`JInternalFrame`のタイトルバー上でも背面にある`JInternalFrame`に反応してドロップ可能になってしまう
- `TransferHandler#canImport(...)`をオーバーライドしてドラッグ元とドロップ先の`JInternalFrame`の重なる共通領域では背面にドロップ出来ないように変更
* Reference [#reference]
- [[JTableの行を別のJTableにドラッグして移動>Swing/DragRowsAnotherTable]]
* Comment [#comment]
#comment
#comment