TITLE:JTabbedPaneの余白に文字列を表示
#navi(../)
RIGHT:Posted by &author(aterai); at 2005-12-26
*JTabbedPaneの余白に文字列を表示 [#d14a047c]
JTabbedPaneの右側の余白に文字列を表示します。[http://forums.sun.com/thread.jspa?threadID=605786 Swing - JTabbedPane with non-tabbed text]の投稿からソースコードを引用しています。

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

//#screenshot
#ref(http://lh4.ggpht.com/_9Z4BYR88imo/TQTUTbAqf_I/AAAAAAAAAl0/APOrWdnvDko/s800/TabbedPaneWithText.png)

**サンプルコード [#f6205526]
#code(link){{
tab = new JTabbedPane() {
  @Override protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    String text = "←ちょっとしたタブの説明など";
    FontMetrics fm = getFontMetrics(getFont());
    int stringWidth = fm.stringWidth(text)+10;
    int x = getSize().width-stringWidth;
    Rectangle lastTab = getUI().getTabBounds(this, getTabCount()-1);
    int tabEnd = lastTab.x + lastTab.width;
    if(x<tabEnd) x = tabEnd;
    g.drawString(text, x+5, 18);
  }
};
}}

**解説 [#t35e18f2]
JTabbedPane#paintComponentメソッドをオーバーライドして、タブコンポーネントの右側の余白に文字列を描画しています。

右端に十分な余白が無く、文字列を描画するとタブ上に重なってしまう場合は、最後のタブの横から文字列を描画するようになっています。

**参考リンク [#w33aebb6]
-[http://forums.sun.com/thread.jspa?threadID=605786 Swing - JTabbedPane with non-tabbed text]
-[[JTabbedPaneの余白にJCheckBoxを配置>Swing/TabbedPaneWithCheckBox]]

**コメント [#q1e57b08]
#comment