Swing/TabWithCloseIcon のバックアップソース(No.21)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/TabWithCloseIcon へ行く。
- 1 (2006-03-20 (月) 12:44:58)
- 2 (2006-03-21 (火) 15:06:03)
- 3 (2006-06-12 (月) 11:42:47)
- 4 (2006-08-17 (木) 18:57:45)
- 5 (2006-08-18 (金) 08:15:54)
- 6 (2006-09-01 (金) 14:54:14)
- 7 (2007-01-31 (水) 15:36:48)
- 8 (2007-03-09 (金) 01:27:32)
- 9 (2007-07-26 (木) 17:12:09)
- 10 (2007-07-26 (木) 23:18:33)
- 11 (2010-09-02 (木) 08:06:15)
- 12 (2012-08-17 (金) 18:36:47)
- 13 (2013-03-13 (水) 15:35:44)
- 14 (2013-08-27 (火) 18:06:51)
- 15 (2013-09-17 (火) 14:44:02)
- 16 (2014-11-25 (火) 03:03:31)
- 17 (2015-02-04 (水) 18:51:22)
- 18 (2016-12-06 (火) 15:08:38)
- 19 (2017-12-03 (日) 00:02:27)
- 20 (2019-08-27 (火) 16:11:28)
- 21 (2021-04-17 (土) 02:57:32)
--- 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`にタブを閉じるためのアイコンやボタンを追加します。以下の参考リンクから引用したコードをほぼそのまま引用して紹介しています。 #download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTVFao3q4I/AAAAAAAAAnE/SarJyg-AIQk/s800/TabWithCloseIcon.png) * サンプルコード [#sourcecode] #code(link){{ public class JTabbedPaneWithCloseIcons extends JTabbedPane { public JTabbedPaneWithCloseIcons() { super(); addMouseListener(new MouseAdapter() { @Override 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); } } } }} * 解説 [#explanation] - 上: `JTabbedPaneWithCloseButton` -- `TabbedPaneLayout`を使用してボタンをタブの中にレイアウト - 中: `JTabbedPaneWithCloseIcons` -- `JTabbedPane`のタブにアイコンを表示する機能を利用 -- `JTabbedPane`に`MouseListener`を設定し、タブのクリックされた位置がアイコン上であればそのタブを閉じる - 下: `CloseableTabbedPane` -- `JTabbedPaneWithCloseIcons`の改良版 -- アイコンの位置、マウスがアイコン上に来たときの描画機能などを追加 ---- `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`を設定可能になったので、上記のサンプルより手軽に同様の機能を実装できるようになりました。 - [[JTabbedPaneにタブを閉じるボタンを追加>Swing/TabWithCloseButton]] - [http://www.02.246.ne.jp/~torutk/jvm/mustang.html Java SE 6 Mustangの新機能] * 参考リンク [#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] * コメント [#comment] #comment #comment