• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JTabbedPaneで選択したタブの高さを変更
#navi(../)
#tags(JTabbedPane)
RIGHT:Posted by &author(aterai); at 2010-04-05
*JTabbedPaneで選択したタブの高さを変更 [#dd299fb8]
``JTabbedPane``で選択したタブの高さを変更します。
---
title: JTabbedPaneで選択したタブの高さを変更
tags: [JTabbedPane]
author: aterai
pubdate: 2010-04-05T04:28:58+09:00
description: JTabbedPaneで選択したタブの高さを変更します。
---
* 概要 [#dd299fb8]
`JTabbedPane`で選択したタブの高さを変更します。

-&jnlp;
-&jar;
-&zip;
#download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTS0RHzbTI/AAAAAAAAAjY/__rqkPO3bsk/s800/SelectedTabHeight.png)

//#screenshot
#ref(http://lh5.ggpht.com/_9Z4BYR88imo/TQTS0RHzbTI/AAAAAAAAAjY/__rqkPO3bsk/s800/SelectedTabHeight.png)

**サンプルコード [#qcce822e]
* サンプルコード [#qcce822e]
#code(link){{
tabbedPane.setUI(new com.sun.java.swing.plaf.windows.WindowsTabbedPaneUI() {
  @Override protected int calculateTabHeight(int tabPlacement, int tabIndex, int fontHeight) {
    return 32;
  private static final int tabAreaHeight = 32;
  @Override protected int calculateTabHeight(
      int tabPlacement, int tabIndex, int fontHeight) {
    return tabAreaHeight;
  }
  @Override protected void paintTab(Graphics g, int tabPlacement, Rectangle[] rects,
                                    int tabIndex, Rectangle iconRect, Rectangle textRect) {
    int selectedIndex  = tabPane.getSelectedIndex();
    boolean isSelected = selectedIndex == tabIndex;
    if(!isSelected) {
      //JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT
      rects[tabIndex].y = 16;
      rects[tabIndex].height = 16;
  @Override protected void paintTab(
      Graphics g, int tabPlacement, Rectangle[] rects,
      int tabIndex, Rectangle iconRect, Rectangle textRect) {
    if(tabPane.getSelectedIndex()!=tabIndex &&
       tabPlacement!=JTabbedPane.LEFT &&
       tabPlacement!=JTabbedPane.RIGHT) {
      int tabHeight = tabAreaHeight/2 + 3;
      rects[tabIndex].height = tabHeight;
      if(tabPlacement==JTabbedPane.TOP) {
        rects[tabIndex].y = tabAreaHeight - tabHeight + 3;
      }
    }
    super.paintTab(g,tabPlacement,rects,tabIndex,iconRect,textRect);
    super.paintTab(g, tabPlacement, rects, tabIndex, iconRect, textRect);
  }
});
}}

**解説 [#ebec32af]
* 解説 [#ebec32af]
上記のサンプルでは、選択されていないタブの高さを低くすることで、選択されたタブの高さが目立つように設定しています。

- 対応しているのは、``JTabbedPane.TOP``, ``JTabbedPane.SCROLL_TAB_LAYOUT``の場合のみ
- `BasicTabbedPaneUI#calculateTabHeight(...)`などをオーバーライドして、タブ(領域)の高さを変更
- `BasicTabbedPaneUI#paintTab(...)`などをオーバーライドして、描画されるタブの高さを`BasicTabbedPaneUI#calculateTabHeight(...)`で設定した高さの半分程度に変更
-- `JTabbedPane.TOP`の場合、選択されていないタブの`y`座標を下に移動
- 対応しているのは、`JTabbedPane.SCROLL_TAB_LAYOUT`の場合のみ
-- `JTabbedPane.TOP`と`JTabbedPane.BOTTOM`の場合、選択したタブの高さが変化する
-- `JTabbedPane.LEFT`と`JTabbedPane.RIGHT`の場合、すべてのタブが`BasicTabbedPaneUI#calculateTabHeight(...)`で設定した高さになる

//**参考リンク
**コメント [#b624a66d]
----
注: 以下のようにタブの位置を変更する`JComboBox`を追加したので、`JDK 1.7.0`以上が必要

#code{{
private static enum TabPlacements {
  TOP(JTabbedPane.TOP), BOTTOM(JTabbedPane.BOTTOM),
  LEFT(JTabbedPane.LEFT), RIGHT(JTabbedPane.RIGHT);
  public final int tabPlacement;
  private TabPlacements(int tabPlacement) {
    this.tabPlacement = tabPlacement;
  }
}
private final JComboBox<TabPlacements> comboBox =
    new JComboBox<>(TabPlacements.values());
private final JTabbedPane tabbedPane = new JTabbedPane(
    JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
//...
comboBox.addItemListener(new ItemListener() {
  @Override public void itemStateChanged(ItemEvent e) {
    if(e.getStateChange()==ItemEvent.SELECTED) {
      tabbedPane.setTabPlacement(((TabPlacements)e.getItem()).tabPlacement);
    }
  }
});
}}

//* 参考リンク
* コメント [#b624a66d]
#comment
#comment