Swing/DnDTabbedPane のバックアップの現在との差分(No.3)
TITLE:JTabbedPaneのタブをドラッグ&ドロップ
JTabbedPaneのタブをドラッグ&ドロップ
編集者:Terai Atsuhiro~
作成日:2004-09-24
更新日:2024-02-24 (土) 05:18:55
- category: swing folder: DnDTabbedPane title: JTabbedPaneのタブをドラッグ&ドロップ tags: [JTabbedPane, DragAndDrop, GlassPane, DragGestureListener] author: aterai pubdate: 2004-09-27T11:54:33+09:00 description: JTabbedPaneのタブをDrag&Dropで移動します。 image: hreflang:
href: https://java-swing-tips.blogspot.com/2008/04/drag-and-drop-tabs-in-jtabbedpane.html lang: en
概要
JTabbedPane
のタブをDrag&Drop
で移動します。
Screenshot
Advertisement
概要
JTabbedPaneのタブをDrag&Dropで移動します。サンプルコード
#spanend
#spanadd
protected int getTargetTabIndex(Point glassPt) {
#spanend
int count = getTabCount();
if (count == 0) {
return -1;
}
#spandel
#screenshot
#spanend
Point tabPt = SwingUtilities.convertPoint(glassPane, glassPt, this);
boolean isHorizontal = isTopBottomTabPlacement(getTabPlacement());
for (int i = 0; i < count; ++i) {
Rectangle r = getBoundsAt(i);
#spandel
**サンプルコード [#jf8c35ef]
#spanend
#spandel
#code{{
#spanend
private int getTargetTabIndex(Point pt) {
for(int i=0;i<this.getTabCount();i++) {
Rectangle rect = this.getBoundsAt(i);
rect.setRect(rect.x-rect.width/2, rect.y, rect.width, rect.height);
if(rect.contains(pt)) {
return i;
}
}
Rectangle rect = this.getBoundsAt(this.getTabCount()-1);
rect.setRect(rect.x+rect.width/2, rect.y, rect.width+100, rect.height);
if(rect.contains(pt)) {
return this.getTabCount();
}else{
return -1;
}
}
// First half.
if (isHorizontal) {
r.width = r.width / 2 + 1;
} else {
r.height = r.height / 2 + 1;
}
if (r.contains(tabPt)) {
return i;
}
private void convertTab(int prev, int next) {
if(next<0 || prev==next) {
return;
}
Component cmp = this.getComponentAt(prev);
String str = this.getTitleAt(prev);
if(next==this.getTabCount()) {
this.remove(prev);
this.addTab(str, cmp);
this.setSelectedIndex(this.getTabCount()-1);
}else if(prev>next) {
this.remove(prev);
this.insertTab(str, null, cmp, null, next);
this.setSelectedIndex(next);
}else{
this.remove(prev);
this.insertTab(str, null, cmp, null, next-1);
this.setSelectedIndex(next-1);
}
}
#spandel
View in GitHub: Java, Kotlin- &jnlp;
- &jar;
- &zip;
// 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); r.translate(r.width * d.x / 2, r.height * d.y / 2); return r.contains(tabPt) ? getTabCount() : -1;
}
解説
上記のサンプルでは、JTabbedPaneのタブをドラッグすると、マウスカーソルが変更されて、ドロップされる位置に青い線を描画します。 private void convertTab(int prev, int 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; 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); }
// 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);} }}
参考リンク
解説
上記のサンプルでは、JTabbedPane
のタブをドラッグするとマウスカーソルが変更されて、ドロップ可能な位置に青い線を描画します。
コメント
- ドラッグ中のタブゴーストを表示する機能を追加しました。 -- terai
- java.awt.dnd パッケージを使用する方法にソースを変更しました。 -- terai
- ドラッグ中に半透明のタブゴーストを表示するかどうかを切り替え可能
- タブ領域以外にドロップしようとするとカーソルが変化
-
JTabbedPane
のタブが二段以上になる場合は未検証 -
MouseMotionListener
とMouseListener
ではなく、DragGestureListener
、DragSourceListener
、DropTargetListener
を使用する方法に変更 - DnDTabbedPane. Idx for insertion selection fix. by AndreiKud · Pull Request #24 · aterai/java-swing-tips
- 短いタブの前後にドロップする場合にバグがあったので、AndreiKudさんのプルリクをコミット
参考リンク
- Java Swing Hacks #63 半透明のドラッグ&ドロップ
- CardLayoutを使ってJTabbedPane風のコンポーネントを作成
- JTabbedPane間でタブのドラッグ&ドロップ移動
- JLayerを使ってJTabbedPaneのタブの挿入位置を描画する
- JTabbedPaneのタブがフレーム外にドロップされたら新規JFrameを作成する