Swing/TabWithCloseButton のバックアップソース(No.14)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/TabWithCloseButton へ行く。
- 1 (2007-01-29 (月) 16:15:06)
- 2 (2007-01-29 (月) 20:54:34)
- 3 (2007-06-12 (火) 19:09:47)
- 4 (2007-07-26 (木) 17:11:08)
- 5 (2010-08-10 (火) 16:47:33)
- 6 (2010-12-09 (木) 02:18:25)
- 7 (2011-07-21 (木) 17:29:16)
- 8 (2013-02-10 (日) 00:03:02)
- 9 (2013-09-17 (火) 14:43:19)
- 10 (2013-10-10 (木) 11:45:07)
- 11 (2015-03-08 (日) 19:37:51)
- 12 (2016-05-23 (月) 01:05:07)
- 13 (2016-07-06 (水) 15:58:16)
- 14 (2017-04-04 (火) 14:17:08)
- 15 (2017-09-26 (火) 11:04:31)
- 16 (2019-04-03 (水) 19:29:53)
- 17 (2021-01-19 (火) 19:34:27)
- 18 (2023-10-06 (金) 15:35:46)
--- category: swing folder: TabWithCloseButton title: JTabbedPaneにタブを閉じるボタンを追加 tags: [JTabbedPane, JButton] author: aterai pubdate: 2007-01-29T16:15:06+09:00 description: JDK 6の新機能を使用し、JTabbedPaneのタブ内に自身を閉じるためのボタンを追加します。 image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTVCzHeo0I/AAAAAAAAAnA/hnMCEbHXnnw/s800/TabWithCloseButton.png --- * 概要 [#summary] `JDK 6`の新機能を使用し、`JTabbedPane`のタブ内に自身を閉じるためのボタンを追加します。 #download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTVCzHeo0I/AAAAAAAAAnA/hnMCEbHXnnw/s800/TabWithCloseButton.png) * サンプルコード [#sourcecode] #code(link){{ class CloseButtonTabbedPane extends JTabbedPane { private final Icon icon; private final Dimension buttonSize; public CloseButtonTabbedPane(Icon icon) { super(); //icon = new CloseTabIcon(); this.icon = icon; buttonSize = new Dimension(icon.getIconWidth(), icon.getIconHeight()); } @Override public void addTab(String title, final JComponent content) { JPanel tab = new JPanel(new BorderLayout()); tab.setOpaque(false); JLabel label = new JLabel(title); label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 4)); JButton button = new JButton(icon); button.setPreferredSize(buttonSize); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { removeTabAt(indexOfComponent(content)); } }); tab.add(label, BorderLayout.WEST); tab.add(button, BorderLayout.EAST); tab.setBorder(BorderFactory.createEmptyBorder(2, 1, 1, 1)); super.addTab(null, content); setTabComponentAt(getTabCount() - 1, tab); } } }} * 解説 [#explanation] `JDK 6`から追加されたタブにコンポーネントを配置する機能を使って、`JButton`をタブに追加しています。 [[以前のサンプル>Swing/TabWithCloseIcon]]に比べると、実装も簡単でコードも短くなっています。 * 参考リンク [#reference] - [http://www.oracle.com/technetwork/articles/javase/index-135776.html More Enhancements in Java SE 6] - [http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html How to Use Tabbed Panes (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)] - [[JTabbedPaneにタブを閉じるアイコンを追加>Swing/TabWithCloseIcon]] - [[JTabbedPaneのTabTitleを左揃えに変更>Swing/TabTitleAlignment]] * コメント [#comment] #comment #comment