TITLE:JTabbedPaneで選択したタブの高さを変更
Posted by terai at 2010-04-05

JTabbedPaneで選択したタブの高さを変更

JTabbedPaneで選択したタブの高さを変更します。
  • category: swing folder: SelectedTabHeight title: JTabbedPaneで選択したタブの高さを変更 tags: [JTabbedPane] author: aterai pubdate: 2010-04-05T04:28:58+09:00 description: JTabbedPaneで選択したタブの高さを変更します。 image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTS0RHzbTI/AAAAAAAAAjY/__rqkPO3bsk/s800/SelectedTabHeight.png hreflang:
       href: https://java-swing-tips.blogspot.com/2010/04/jtabbedpane-selected-tab-height.html
       lang: en

概要

JTabbedPaneで選択したタブの高さを変更します。

#screenshot

サンプルコード

#spanend
#spanadd
class WindowsTabHeightTabbedPaneUI extends WindowsTabbedPaneUI {
#spanend
  private static final int TAB_AREA_HEIGHT = 32;
  @Override protected int calculateTabHeight(
      int tabPlacement, int tabIndex, int fontHeight) {
    return TAB_AREA_HEIGHT;
  }
  @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 = TAB_AREA_HEIGHT / 2 + 3;
      rects[tabIndex].height = tabHeight;
      if (tabPlacement == JTabbedPane.TOP) {
        rects[tabIndex].y = TAB_AREA_HEIGHT - tabHeight + 3;
      }
    }
    super.paintTab(g, tabPlacement, rects, tabIndex, iconRect, textRect);
  }
#spanadd
}
#spanend
#spanadd
View in GitHub: Java, Kotlin

サンプルコード

解説

上記のサンプルでは、選択されていないタブの高さを低くすることで、選択されたタブの高さが目立つように設定しています。
  • BasicTabbedPaneUI#calculateTabHeight(...)などをオーバーライドして、タブ領域の高さを変更
  • BasicTabbedPaneUI#paintTab(...)などをオーバーライドして、描画されるタブの高さをBasicTabbedPaneUI#calculateTabHeight(...)で設定した高さの半分程度に変更
    • JTabbedPane.TOPの場合、選択されていないタブのy座標を下に移動
  • 対応しているのはJTabbedPane.SCROLL_TAB_LAYOUTの場合のみ
    • JTabbedPane.TOPJTabbedPane.BOTTOMの場合、選択したタブの高さが変化する
    • JTabbedPane.LEFTJTabbedPane.RIGHTの場合、すべてのタブがBasicTabbedPaneUI#calculateTabHeight(...)で設定した高さになる
  • -
  • タブの位置を変更するJComboBoxを追加
    #spandel
    tabbedPane.setUI(new com.sun.java.swing.plaf.windows.WindowsTabbedPaneUI() {
    #spanend
      @Override protected int calculateTabHeight(int tabPlacement, int tabIndex, int fontHeight) {
        return 32;
    #spanadd
    private static enum TabPlacements {
    #spanend
      TOP(JTabbedPane.TOP), BOTTOM(JTabbedPane.BOTTOM),
      LEFT(JTabbedPane.LEFT), RIGHT(JTabbedPane.RIGHT);
      public final int tabPlacement;
      private TabPlacements(int tabPlacement) {
        this.tabPlacement = tabPlacement;
      }
      @Override protected void paintTab(Graphics g, int tabPlacement, Rectangle[] rects,
                                        int tabIndex, Rectangle iconRect, Rectangle textRect) {
        Rectangle tabRect  = rects[tabIndex];
        int selectedIndex  = tabPane.getSelectedIndex();
        boolean isSelected = selectedIndex == tabIndex;
        if(!isSelected) {
          //JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT
          rects[tabIndex].y = 16;
          rects[tabIndex].height = 16;
    #spanadd
    }
    #spanend
    #spanadd
    private final JComboBox<TabPlacements> comboBox =
    #spanend
        new JComboBox<>(TabPlacements.values());
    #spanadd
    private final JTabbedPane tabbedPane = new JTabbedPane(
    #spanend
        JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
    #spanadd
    // ...
    #spanend
    #spanadd
    comboBox.addItemListener(new ItemListener() {
    #spanend
      @Override public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {
          tabbedPane.setTabPlacement(((TabPlacements) e.getItem()).tabPlacement);
        }
        super.paintTab(g,tabPlacement,rects,tabIndex,iconRect,textRect);
      }
    });
    

解説

上記のサンプルでは、選択されていないタブの高さを低くすることで、選択されたタブの高さが目立つように設定しています。
  • 対応しているのは、JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT の場合のみ

参考リンク

コメント

コメント