TITLE:JTabbedPaneのタブをドラッグ&ドロップ

JTabbedPaneのタブをドラッグ&ドロップ

Posted by terai at 2004-09-24
  • 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: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTLjYzYe0I/AAAAAAAAAXw/nr90t9LvfMI/s800/DnDTabbedPane.png hreflang:
       href: https://java-swing-tips.blogspot.com/2008/04/drag-and-drop-tabs-in-jtabbedpane.html
       lang: en

概要

JTabbedPaneのタブをDrag&Dropで移動します。

概要

JTabbedPaneのタブをDrag&Dropで移動します。

サンプルコード

#spanend
#spanadd
protected int getTargetTabIndex(Point glassPt) {
#spanend
  int count = getTabCount();
  if (count == 0) {
    return -1;
  }

-&jnlp;
-&jar;
-&zip;
  Point tabPt = SwingUtilities.convertPoint(glassPane, glassPt, this);
  boolean isHorizontal = isTopBottomTabPlacement(getTabPlacement());
  for (int i = 0; i < count; ++i) {
    Rectangle r = getBoundsAt(i);

#spandel
#screenshot
#spanend
    // First half.
    if (isHorizontal) {
      r.width = r.width / 2 + 1;
    } else {
      r.height = r.height / 2 + 1;
    }
    if (r.contains(tabPt)) {
      return i;
    }

#spandel
**サンプルコード [#jf8c35ef]
#spanend
#spandel
#code{{
#spanend
#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++) {
    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;
    // 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 = 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);
  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);
#spanadd

#spanend
  // When you drag'n'drop a disabled tab, it finishes enabled and selected.
  // pointed out by dlorde
  if (flg) {
    setSelectedIndex(tgtindex);
  }
#spanadd

#spanend
  // 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のタブが二段以上になる場合は未検証
  • MouseMotionListenerMouseListenerではなく、DragGestureListenerDragSourceListenerDropTargetListenerを使用する方法に変更
  • DnDTabbedPane. Idx for insertion selection fix. by AndreiKud · Pull Request #24 · aterai/java-swing-tips
    • 短いタブの前後にドロップする場合にバグがあったので、AndreiKudさんのプルリクをコミット
MouseMotionListenerとMouseListenerをimplementsしたJTabbedPaneを作成しています。 JTabbedPaneのタブが二段以上になる場合の検証はほとんどしていません。

参考リンク

参考リンク

コメント