• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JTabbedPaneのタイトルをクリップ
#navi(../)
RIGHT:Posted by [[terai]] at 2007-10-08
*JTabbedPaneのタイトルをクリップ [#paeaad58]
JDK 6 で導入されたタブにコンポーネントを追加する機能を使って、長いタイトルのタブは文字列をクリップして表示します。

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

#screenshot

**サンプルコード [#xad70fa1]
#code{{
class ClippedTitleTabbedPane extends JTabbedPane {
  private boolean first_flag = true;
  public ClippedTitleTabbedPane() {
    super();
    addComponentListener(new ComponentAdapter() {
      @Override public void componentResized(ComponentEvent e) {
        if(first_flag) {
          initTabWidth();
          first_flag = false;
        }
      }
    });
  }
  public ClippedTitleTabbedPane(int tabPlacement) {
    super(tabPlacement);
  }
  private Insets getTabInsets() {
    Insets i = UIManager.getInsets("TabbedPane.tabInsets");
    if(i!=null) {
      return i;
    }else{
      SynthStyle style = SynthLookAndFeel.getStyle(this, Region.TABBED_PANE_TAB);
      SynthContext context = new SynthContext(this, Region.TABBED_PANE_TAB,
                                              style, SynthConstants.ENABLED);
      SynthContext context = new SynthContext(
          this, Region.TABBED_PANE_TAB, style, SynthConstants.ENABLED);
      return style.getInsets(context, null);
    }
  }
  private Insets getTabAreaInsets() {
    Insets i = UIManager.getInsets("TabbedPane.tabAreaInsets");
    if(i!=null) {
      return i;
    }else{
      SynthStyle style = SynthLookAndFeel.getStyle(this, Region.TABBED_PANE_TAB_AREA);
      SynthContext context = new SynthContext(this, Region.TABBED_PANE_TAB_AREA,
                                              style, SynthConstants.ENABLED);
      SynthContext context = new SynthContext(
          this, Region.TABBED_PANE_TAB_AREA, style, SynthConstants.ENABLED);
      return style.getInsets(context, null);
    }
  }
  private void initTabWidth() {
  @Override public void doLayout() {
    int tabCount  = getTabCount();
    if(tabCount==0) return;
    Insets tabInsets   = getTabInsets();
    Insets tabAreaInsets = getTabAreaInsets();
    Insets insets = getInsets();
    int areaWidth = getWidth() - tabAreaInsets.left - tabAreaInsets.right
                               - insets.left - insets.right;
                               - insets.left        - insets.right;
    int tabWidth  = 0; // = tabInsets.left + tabInsets.right + 3;
    int gap     = 0;

    switch(getTabPlacement()) {
      case LEFT: case RIGHT:
      tabWidth = areaWidth / 4;
      gap = 0;
      break;
      case BOTTOM: case TOP: default:
      tabWidth = areaWidth / tabCount;
      gap = areaWidth - (tabWidth * tabCount);
    }
    // "3" is magic number @see BasicTabbedPaneUI#calculateTabWidth
    tabWidth = tabWidth - tabInsets.left - tabInsets.right - 3;
    for(int i=0;i<tabCount;i++) {
      JLabel l = (JLabel)getTabComponentAt(i);
      if(l==null) break;
      l.setPreferredSize(new Dimension(tabWidth+(i<gap?1:0), l.getPreferredSize().height));
    }
    super.doLayout();
  }
  @Override
  public synchronized void repaint() {
    initTabWidth();
    super.repaint();
  }
  @Override
  public void insertTab(String title, Icon icon, Component component, String tip, int index) {
    super.insertTab(title, icon, component, tip==null?title:tip, index);
    JLabel label = new JLabel(title, JLabel.CENTER);
    Dimension dim = label.getPreferredSize();
    Insets tabInsets = getTabInsets();
    label.setPreferredSize(new Dimension(0, dim.height+tabInsets.top+tabInsets.bottom));
    setTabComponentAt(index, label);
  }
}
}}

**解説 [#xe7c3559]
下のJTabbedPaneでは、タブにJTabbedPane#setTabComponentAtメソッドを使ってJLabelを追加し、そのクリップ機能を利用して長いタイトル文字列をクリップしています。

[[JTabbedPaneのタブを等幅にしてタイトルをクリップ>Swing/ClippedTitleTab]]と、ほぼ同等((文字列の長さがばらばらでも、左右にタブをおいた場合は全体の1/4の幅に、上下にタブをおいた場合はすべてのタブ幅が均等になる))ですが、TabbedPaneUI#paintTextで文字列の描画をオーバーライドする必要も無く、ソースも短くて実装が簡単です。

**参考リンク [#r6054f8d]
-[[JTabbedPaneのタブを等幅にしてタイトルをクリップ>Swing/ClippedTitleTab]]

**コメント [#p47877ec]
- tabAreaInsetsを考慮するように修正し、TOP-LEFTの切り替え機能を追加しました。 -- [[terai]] &new{2008-02-26 (火) 22:15:27};
- Synthに仮?対応。 -- [[terai]] &new{2008-03-22 (土) 12:49:05};
-- GTK L&F に対応するのは、JDK 1.7 以降になる予定?です。 [[Bug ID: 6354790 GTK LAF: Painting bugs in JTabbedPane>http://bugs.sun.com/view_bug.do?bug_id=6354790]] -- [[terai]] &new{2008-03-24 (月) 17:27:13};
- JTabbedPane#doLayout()をオーバーライドするように変更。 -- [[terai]] &new{2010-10-08 (金) 15:47:34};

#comment