• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JTabbedPaneにタブを閉じるアイコンを追加
#navi(../)
*JTabbedPaneにタブを閉じるアイコンを追加 [#x7fdca76]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2006-03-20~
更新日:&lastmod;
---
category: swing
folder: TabWithCloseIcon
title: JTabbedPaneにタブを閉じるアイコンを追加
tags: [JTabbedPane, Icon, JButton]
author: aterai
pubdate: 2006-03-20T12:44:58+09:00
description: JTabbedPaneにタブを閉じるためのアイコンやボタンを追加します。
image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTVFao3q4I/AAAAAAAAAnE/SarJyg-AIQk/s800/TabWithCloseIcon.png
---
* 概要 [#summary]
`JTabbedPane`にタブを閉じるためのアイコンやボタンを追加します。以下の参考リンクから引用したコードをほぼそのまま引用して紹介しています。

#contents
#download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTVFao3q4I/AAAAAAAAAnE/SarJyg-AIQk/s800/TabWithCloseIcon.png)

**概要 [#x36e4e2a]
JTabbedPaneにタブを閉じるためのアイコンやボタンを追加します。以下の参考リンクから引用したコードをほぼそのまま引用して紹介しています。
* サンプルコード [#sourcecode]
#code(link){{
public class JTabbedPaneWithCloseIcons extends JTabbedPane {
  public JTabbedPaneWithCloseIcons() {
    super();
    addMouseListener(new MouseAdapter() {
      @Override public void mouseClicked(MouseEvent e) {
        tabClicked(e);
      }
    });
  }

#screenshot
  public void addTab(String title, Component component) {
    this.addTab(title, component, null);
  }

**サンプルコード [#b602abb2]
#code{{
 public class JTabbedPaneWithCloseIcons extends JTabbedPane {
   public JTabbedPaneWithCloseIcons() {
     super();
     addMouseListener(new MouseAdapter() {
       public void mouseClicked(MouseEvent e) {
         tabClicked(e);
       }
     });
   }
   public void addTab(String title, Component component) {
     this.addTab(title, component, null);
   }
   public void addTab(String title, Component component, Icon extraIcon) {
     super.addTab(title, new CloseTabIcon(extraIcon), component);
   }
   private void tabClicked(MouseEvent e) {
     int index = getUI().tabForCoordinate(this, e.getX(), e.getY());
     if(index<0) return;
     Rectangle rect=((CloseTabIcon)getIconAt(index)).getBounds();
     if(rect.contains(e.getX(), e.getY())) {
       removeTabAt(index);
     }
   }
 }
  public void addTab(String title, Component component, Icon extraIcon) {
    super.addTab(title, new CloseTabIcon(extraIcon), component);
  }

  private void tabClicked(MouseEvent e) {
    int index = getUI().tabForCoordinate(this, e.getX(), e.getY());
    if (index < 0) {
      return;
    }
    Rectangle rect = ((CloseTabIcon) getIconAt(index)).getBounds();
    if (rect.contains(e.getX(), e.getY())) {
      removeTabAt(index);
    }
  }
}
}}
-&jnlp;
-&jar;
-&zip;

**解説 [#p2450b3a]
-JTabbedPaneWithCloseButton(上)
>TabbedPaneLayoutを使用して、ボタンをタブの中にレイアウトしています。
* 解説 [#explanation]
- 上: `JTabbedPaneWithCloseButton`
-- `TabbedPaneLayout`を使用してボタンをタブの中にレイアウト
- 中: `JTabbedPaneWithCloseIcons`
-- `JTabbedPane`のタブにアイコンを表示する機能を利用
-- `JTabbedPane`に`MouseListener`を設定し、タブのクリックされた位置がアイコン上であればそのタブを閉じる
- 下: `CloseableTabbedPane`
-- `JTabbedPaneWithCloseIcons`の改良版
-- アイコンの位置、マウスがアイコン上に来たときの描画機能などを追加

-JTabbedPaneWithCloseIcons(中)
>JTabbedPaneには、タブにアイコンを表示する機能があるので、これを利用しています。タブのクリックされた位置がアイコン上かどうかで、そのタブを閉じるかどうかを判断しています。
----
`JDK 1.6.0`では、`JTabbedPane`のタブ部分に[https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JTabbedPane.html#setTabComponentAt-int-java.awt.Component- JTabbedPane#setTabComponentAt(Component)]メソッドで`Component`を設定可能になったので、上記のサンプルより手軽に同様の機能を実装できるようになりました。

-CloseableTabbedPane(下)
>JTabbedPaneWithCloseIconsの改良版です。アイコンの位置、マウスがアイコン上に来たときの描画機能などが追加されています。
- [[JTabbedPaneにタブを閉じるボタンを追加>Swing/TabWithCloseButton]]
- [http://www.02.246.ne.jp/~torutk/jvm/mustang.html Java SE 6 Mustangの新機能]

上記のサンプルコードは、一番簡単なJTabbedPaneWithCloseIconsのものを掲載しています。
* 参考リンク [#reference]
- [https://community.oracle.com/thread/1501884 Swing (Archive) - Adding a close icon to a JTabbedPane tab]
- [https://community.oracle.com/thread/1356993 Swing - JTabbedPane with close Icons]
- [https://community.oracle.com/thread/1480617 Swing (Archive) - Closable Tab in JTabbedPane]
- [http://www.javaworld.com/javaworld/jw-09-2004/jw-0906-tabbedpane.html CloseAndMaxTabbedPane: An enhanced JTabbedPane]
- [http://www.infonode.net/index.html?itp InfoNode - Java Components]
- [http://weblogs.java.net/blog/kirillcool/archive/2005/12/spicing_up_your_1.html Kirill Grouchnikov's Blog: Spicing up your JTabbedPane - part II]
- [[JTabbedPaneでタブを追加削除>Swing/TabbedPane]]
- [http://www.oracle.com/technetwork/articles/javase/index-135776.html More Enhancements in Java SE 6]

Java SE 6 では、JTabbedPaneのタブ部分に、文字列・アイコンに加えSwingコンポーネントが使えるようになっているので、上記のサンプルはもっと簡単に実現できるようになっています。
-[[JTabbedPaneにタブを閉じるボタンを追加>Swing/TabWithCloseButton]]
-[[Java SE 6 Mustangの新機能>http://www.02.246.ne.jp/~torutk/jvm/mustang.html]]

**参考リンク [#l8a5ea56]
-[[Adding a close icon to a JTabbedPane tab>http://forum.java.sun.com/thread.jspa?threadID=239270]]
-[[JTabbedPane with close Icons>http://forum.java.sun.com/thread.jspa?threadID=337070]]
-[[Closable Tab in JTabbedPane>http://forum.java.sun.com/thread.jspa?threadID=384894]]
-[[CloseAndMaxTabbedPane: An enhanced JTabbedPane>http://www.javaworld.com/javaworld/jw-09-2004/jw-0906-tabbedpane.html]]
-[[InfoNode - Java Components>http://www.infonode.net/index.html?itp]]
-[[Kirill Grouchnikov's Blog: Spicing up your JTabbedPane - part II>http://weblogs.java.net/blog/kirillcool/archive/2005/12/spicing_up_your_1.html]]
-[[JTabbedPaneでタブを追加削除>Swing/TabbedPane]]
-[[More Enhancements in Java SE 6 (Mustang)>http://java.sun.com/developer/technicalArticles/J2SE/Desktop/mustang/enhancements/?feed=JSC]]

**コメント [#m1834b55]
* コメント [#comment]
#comment
#comment