Swing/DnDTabbedPane のバックアップの現在との差分(No.22)
-
category: swing
folder: DnDTabbedPane
title: JTabbedPaneのタブをドラッグ&ドロップ
tags: [JTabbedPane, DragAndDrop, GlassPane, DragGestureListener]
author: aterai
pubdate: 2004-09-27
pubdate: 2004-09-27T11:54:33+09:00
description: JTabbedPaneのタブをDrag&Dropで移動します。
image:
hreflang:
href: http://java-swing-tips.blogspot.com/2008/04/drag-and-drop-tabs-in-jtabbedpane.html href: https://java-swing-tips.blogspot.com/2008/04/drag-and-drop-tabs-in-jtabbedpane.html lang: en
概要
概要
JTabbedPane
のタブをDrag&Drop
で移動します。
Screenshot
Advertisement
サンプルコード
サンプルコード
#spandel
private int getTargetTabIndex(Point glassPt) {
#spanend
Point tabPt = SwingUtilities.convertPoint(glassPane, glassPt, DnDTabbedPane.this);
boolean isTB = getTabPlacement()==JTabbedPane.TOP || getTabPlacement()==JTabbedPane.BOTTOM;
for(int i=0;i<getTabCount();i++) {
#spanadd
protected int getTargetTabIndex(Point glassPt) {
#spanend
int count = getTabCount();
if (count == 0) {
return -1;
}
#spanadd
#spanend
Point tabPt = SwingUtilities.convertPoint(glassPane, glassPt, this);
boolean isHorizontal = isTopBottomTabPlacement(getTabPlacement());
for (int i = 0; i < count; ++i) {
Rectangle r = getBoundsAt(i);
if(isTB) r.setRect(r.x-r.width/2, r.y, r.width, r.height);
else r.setRect(r.x, r.y-r.height/2, r.width, r.height);
if(r.contains(tabPt)) return i;
#spanadd
#spanend
// First half.
if (isHorizontal) {
r.width = r.width / 2 + 1;
} else {
r.height = r.height / 2 + 1;
}
if (r.contains(tabPt)) {
return i;
}
#spanadd
#spanend
// Second half.
if (isHorizontal) {
r.x += r.width;
} else {
r.y += r.height;
}
if (r.contains(tabPt)) {
return i + 1;
}
}
Rectangle r = getBoundsAt(getTabCount()-1);
if(isTB) r.setRect(r.x+r.width/2, r.y, r.width, r.height);
else r.setRect(r.x, r.y+r.height/2, r.width, r.height);
return r.contains(tabPt)?getTabCount():-1;
Rectangle r = getBoundsAt(getTabCount() - 1);
r.translate(r.width * d.x / 2, r.height * d.y / 2);
return r.contains(tabPt) ? getTabCount() : -1;
}
private void convertTab(int prev, int next) {
if(next<0 || prev==next) {
if (next < 0 || prev == next) {
return;
}
Component cmp = getComponentAt(prev);
Component tab = getTabComponentAt(prev);
String str = getTitleAt(prev);
Icon icon = getIconAt(prev);
String tip = getToolTipTextAt(prev);
boolean flg = isEnabledAt(prev);
int tgtindex = prev>next ? next : next-1;
int tgtindex = prev > next ? next : next - 1;
remove(prev);
insertTab(str, icon, cmp, tip, tgtindex);
setEnabledAt(tgtindex, flg);
//When you drag'n'drop a disabled tab, it finishes enabled and selected.
//pointed out by dlorde
if(flg) setSelectedIndex(tgtindex);
// When you drag'n'drop a disabled tab, it finishes enabled and selected.
// pointed out by dlorde
if (flg) {
setSelectedIndex(tgtindex);
}
// I have a component in all tabs (jlabel with an X to close the tab)
// and when i move a tab the component disappear.
// pointed out by Daniel Dario Morales Salas
setTabComponentAt(tgtindex, tab);
}
View in GitHub: Java, Kotlin解説
上記のサンプルでは、JTabbedPane
のタブをドラッグすると、マウスカーソルが変更されて、ドロップ可能な位置に青い線を描画します。
解説
上記のサンプルでは、JTabbedPane
のタブをドラッグするとマウスカーソルが変更されて、ドロップ可能な位置に青い線を描画します。
ドラッグ中、半透明のタブゴーストを表示するかどうかを切り替えることが出来ます。タブ領域以外にドロップしようとすると、カーソルが変化します。JTabbedPane
のタブが二段以上になる場合の検証はほとんどしていません。
- ドラッグ中に半透明のタブゴーストを表示するかどうかを切り替え可能
- タブ領域以外にドロップしようとするとカーソルが変化
-
JTabbedPane
のタブが二段以上になる場合は未検証 -
MouseMotionListener
とMouseListener
ではなく、DragGestureListener
、DragSourceListener
、DropTargetListener
を使用する方法に変更 - DnDTabbedPane. Idx for insertion selection fix. by AndreiKud · Pull Request #24 · aterai/java-swing-tips
- 短いタブの前後にドロップする場合にバグがあったので、AndreiKudさんのプルリクをコミット
MouseMotionListener
とMouseListener
ではなく、DragGestureListener
、DragSourceListener
、DropTargetListener
を使用する方法に変更しました。
参考リンク
参考リンク
- Java Swing Hacks #63 半透明のドラッグ&ドロップ
- CardLayoutを使ってJTabbedPane風のコンポーネントを作成
- JTabbedPane間でタブのドラッグ&ドロップ移動
- JLayerを使ってJTabbedPaneのタブの挿入位置を描画する
- JTabbedPaneのタブがフレーム外にドロップされたら新規JFrameを作成する