• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:CardLayoutを使ってJTabbedPane風のコンポーネントを作成
#navi(../)
#tags(CardLayout, GridLayout, LayoutManager, JRadioButton, JTabelHeader, JTabbedPane)
#tags(CardLayout, GridLayout, LayoutManager, JRadioButton, JTableHeader, JTabbedPane)
RIGHT:Posted by &author(aterai); at 2008-10-27
*CardLayoutを使ってJTabbedPane風のコンポーネントを作成 [#u2e4ee0a]
``CardLayout``と``JRadioButton``や``JTabelHeader``を組み合わせて``JTabbedPane``風のコンポーネントを作成します。
* CardLayoutを使ってJTabbedPane風のコンポーネントを作成 [#u2e4ee0a]
``CardLayout``と``JRadioButton``や``JTableHeader``を組み合わせて``JTabbedPane``風のコンポーネントを作成します。

-&jnlp;
-&jar;
-&zip;
- &jnlp;
- &jar;
- &zip;

//#screenshot
#ref(http://lh5.ggpht.com/_9Z4BYR88imo/TQTIbe7vt7I/AAAAAAAAASw/SGFMcgCN_r4/s800/CardLayoutTabbedPane.png)
#ref(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTIbe7vt7I/AAAAAAAAASw/SGFMcgCN_r4/s800/CardLayoutTabbedPane.png)

**サンプルコード [#wd961a26]
** サンプルコード [#wd961a26]
#code(link){{
class CardLayoutTabbedPane extends JPanel {
  protected final CardLayout cardLayout = new CardLayout();
  protected final JPanel tabPanel = new JPanel(new GridLayout(1, 0, 0, 0));
  protected final JPanel wrapPanel = new JPanel(new BorderLayout(0, 0));
  protected final JPanel contentsPanel = new JPanel(cardLayout);
  protected final ButtonGroup bg = new ButtonGroup();
  public CardLayoutTabbedPane() {
    super(new BorderLayout());
    int left  = 1;
    int right = 3;
    tabPanel.setBorder(BorderFactory.createEmptyBorder(1,left,0,right));
    contentsPanel.setBorder(BorderFactory.createEmptyBorder(4,left,2,right));
    wrapPanel.add(tabPanel);
    wrapPanel.add(new JLabel("test:"), BorderLayout.WEST);
    add(wrapPanel, BorderLayout.NORTH);
    add(contentsPanel);
  }
  public void addTab(final String title, final Component comp) {
    JRadioButton b = new TabButton(new AbstractAction(title) {
      public void actionPerformed(ActionEvent e) {
        cardLayout.show(contentsPanel, title);
      }
    });
    tabPanel.add(b);
    bg.add(b);
    b.setSelected(true);
    contentsPanel.add(comp, title);
    cardLayout.show(contentsPanel, title);
  }
}
}}

**解説 [#w7802e67]
** 解説 [#w7802e67]
``CardLayout``で``JTabbedPane``風のコンポーネントを作成すると、タブエリアのレイアウトをネストしてコンポーネントの追加したり、レイアウトマネージャーを変更することで、タブの配置を変更することが簡単になります。

-``CardLayout``+``JRadioButton``
--上記のサンプルでは、``JRadioButton``を``GridLayout``で、均等なサイズになるように並べています。
--``UI``を変更して、チェックは非表示にしています。
--マウスでタブを押した時ではなく、放した時に切り替わります%%(``Opera``風)%%。
- ``CardLayout``+``JRadioButton``
-- 上記のサンプルでは、``JRadioButton``を``GridLayout``で、均等なサイズになるように並べています。
-- ``UI``を変更して、チェックは非表示にしています。
-- マウスでタブを押した時ではなく、 %%(``Opera``風に)%% 放した時に切り替わります。

-``CardLayout``+``JTableHeader``
--空の``JTable``を作成して``JTableHeader``を取り出して利用しています。
--``JTableHeader``のドラッグ&ドロップによる入れ替えや、ヘッダ(タブ)幅のリサイズが利用できます。
- ``CardLayout``+``JTableHeader``
-- 空の``JTable``を作成して``JTableHeader``を取り出して利用しています。
-- ``JTableHeader``のドラッグ&ドロップによる入れ替えや、ヘッダ(タブ)幅のリサイズが利用できます。

**参考リンク [#w973281a]
-[[JTabbedPaneのタブをドラッグ&ドロップ>Swing/DnDTabbedPane]]
-[[TabbedPane風のタブ配置をレイアウトマネージャーで変更>Swing/NewTabButton]]
** 参考リンク [#w973281a]
- [[JTabbedPaneのタブをドラッグ&ドロップ>Swing/DnDTabbedPane]]
- [[TabbedPane風のタブ配置をレイアウトマネージャーで変更>Swing/NewTabButton]]

**コメント [#i7cab3fd]
** コメント [#i7cab3fd]
- ``CloseButton``(ダミー)を追加。 -- [[aterai]] &new{2008-10-29 (水) 18:14:02};

#comment