#navi(../)
*JTabbedPaneのタブを等幅にしてタイトルをクリップ [#r616bd2a]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2005-08-08~
更新日:&lastmod;

#contents

**概要 [#fd7b15fa]
JTabbedPaneのタブを等幅にし、長いタイトルはクリップして表示します。

http://terai.xrea.jp/swing/clippedtitle/screenshot.png

**サンプルコード [#z01f21c5]
 tab1.setUI(new javax.swing.plaf.basic.BasicTabbedPaneUI() {
   protected int calculateTabWidth(int tabP, int tabIdx, FontMetrics m) {
     int i = tabPane.getTabCount();
     Insets insets = tabPane.getInsets();
     int width = tabPane.getWidth()-insets.left-insets.right;
     return (int)(width/i)-2;
   }
   protected void paintText(Graphics g, int tabPlacement,
       Font font, FontMetrics metrics, int tabIndex,
       String title, Rectangle textRect, 
       boolean isSelected) {
     Rectangle tabRect = rects[tabIndex];
     String clippedText = SwingUtilities.layoutCompoundLabel(
       (JComponent) tabPane,
       metrics, title, getIconForTab(tabIndex),
       SwingUtilities.CENTER,
       SwingUtilities.CENTER,
       SwingUtilities.CENTER,
       SwingUtilities.TRAILING,
       tabRect,
       new Rectangle(),
       textRect,
       textIconGap);
       super.paintText(g, tabPlacement, font, metrics,
                       tabIndex, clippedText, textRect, isSelected);
 }

-[[サンプルを起動>http://terai.xrea.jp/swing/clippedtitle/sample.jnlp]]
-[[jarファイル>http://terai.xrea.jp/swing/clippedtitle/sample.jar]]
-[[ソース>http://terai.xrea.jp/swing/clippedtitle/src.zip]]

**解説 [#v0f32f18]
上記のサンプルでは、すべてのタブ幅が等しくなるようにし((二段にならないように少しだけ小さくしています))、この幅よりタイトルが長い場合は、SwingUtilities.layoutCompoundLabelメソッドで文字列をクリップして表示しています。タイトルがクリップされていても、ツールチップで元の文字列を表示することができます。

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