Swing/InternalFrameDropTarget のバックアップ(No.5)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/InternalFrameDropTarget へ行く。
- 1 (2014-05-19 (月) 00:37:47)
- 2 (2014-06-27 (金) 15:51:27)
- 3 (2014-10-31 (金) 01:47:25)
- 4 (2015-11-21 (土) 02:37:08)
- 5 (2017-05-15 (月) 14:54:08)
- 6 (2017-08-15 (火) 14:46:23)
- 7 (2018-08-22 (水) 20:04:21)
- 8 (2020-08-18 (火) 05:06:41)
- 9 (2022-01-21 (金) 11:26:01)
- 10 (2025-01-03 (金) 08:57:02)
- 11 (2025-01-03 (金) 09:01:23)
- 12 (2025-01-03 (金) 09:02:38)
- 13 (2025-01-03 (金) 09:03:21)
- 14 (2025-01-03 (金) 09:04:02)
- 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:
概要
JInternalFrame
間でJTable
の行をドラッグ&ドロップを使って入れ替えます。
Screenshot

Advertisement
サンプルコード
private boolean isDropableTableIntersection(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;
}
View in GitHub: Java, Kotlin解説
上記のサンプルでは、異なるJInternalFrame
に配置したJTable
の行をドラッグ&ドロップで入れ替えています。使用するTransferHandler
はJTableの行を別のJTableにドラッグして移動のものとほぼ同じですが、前面のJInternalFrame
内にあるJTable
からドラッグを開始した場合、そのJTableHeader
やJInternalFrame
のタイトルバー上でも、背面にあるJInternalFrame
に反応してドロップ可能になってしまうため、TransferHandler#canImport(...)
をオーバーライドして、ドラッグ元とドロップ先のJInternalFrame
の共通部分では背面にドロップ出来ないように変更しています。