• 追加された行はこの色です。
  • 削除された行はこの色です。
#navi(../)
*DoubleBufferingで自由曲線を描画 [#u6cdf601]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2005-12-26~
更新日:&lastmod;
---
category: swing
folder: TabbedPaneWithText
title: JTabbedPaneの余白に文字列を表示
tags: [JTabbedPane, FontMetrics, JLayer]
author: aterai
pubdate: 2005-12-26T12:36:53+09:00
description: JTabbedPaneのタブエリア右側の余白に文字列を表示します。
image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTUTbAqf_I/AAAAAAAAAl0/APOrWdnvDko/s800/TabbedPaneWithText.png
---
* 概要 [#summary]
`JTabbedPane`のタブエリア右側の余白に文字列を表示します。

#contents
#download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTUTbAqf_I/AAAAAAAAAl0/APOrWdnvDko/s800/TabbedPaneWithText.png)

**概要 [#u6b5c68a]
JTabbedPaneの右側の余白に文字列を表示します。[[Java Forums - JTabbedPane with non-tabbed text>http://forum.java.sun.com/thread.jspa?threadID=605786]]の投稿からソースコードを引用しています。
* サンプルコード [#sourcecode]
#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 = getBoundsAt(getTabCount() - 1);
    int tabEnd = lastTab.x + lastTab.width;
    if (x < tabEnd) {
      x = tabEnd;
    }
    g.drawString(text, x + 5, 18);
  }
};
}}

http://terai.xrea.jp/swing/tabbedpanewithtext/screenshot.png
* 解説 [#explanation]
`JTabbedPane#paintComponent(...)`メソッドをオーバーライドして、タブコンポーネントの右側の余白に文字列を描画しています。

**サンプルコード [#f6205526]
 tab = new JTabbedPane() {
   public 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;
----
- `JDK 1.7.0`以降の場合`JLayer`を使用してラベルを描画する方法もある

     g.drawString(text, x+5, 18);
   }
 };
#code{{
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.*;

-[[サンプルを起動>http://terai.xrea.jp/swing/tabbedpanewithtext/sample.jnlp]]
-[[jarファイル>http://terai.xrea.jp/swing/tabbedpanewithtext/sample.jar]]
-[[ソース>http://terai.xrea.jp/swing/tabbedpanewithtext/src.zip]]
public class TopRightCornerLabelLayerUITest {
  public static JComponent makeUI() {
    JTabbedPane tabs = new JTabbedPane();
    tabs.addTab("New tab1", new JLabel("1"));
    tabs.addTab("New Tab2", new JLabel("2"));
    JPanel p = new JPanel(new BorderLayout());
    p.add(new JLayer<JComponent>(tabs, new TopRightCornerLabelLayerUI()));
    return p;
  }

**解説 [#t35e18f2]
JTabbedPane#paintComponentメソッドをオーバーライドして、タブコンポーネントの右側の余白に文字列を描画しています。
  private static void createAndShowUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(makeUI());
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }

十分な余白が無く、右端から文字列を描画するとタブ上に重なってしまう場合は、最後のタブの横から文字列を描画するようになっています。
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowUI();
      }
    });
  }
}

**参考リンク [#w33aebb6]
-[[Java Forums - JTabbedPane with non-tabbed text>http://forum.java.sun.com/thread.jspa?threadID=605786]]
class TopRightCornerLabelLayerUI extends LayerUI<JComponent> {
  private JLabel l = new JLabel("A Label at right corner");
  private JPanel rubberStamp = new JPanel();
  @Override public void paint(Graphics g, JComponent c) {
    super.paint(g, c);
    Dimension d = l.getPreferredSize();
    int x = c.getWidth() - d.width - 5;
    SwingUtilities.paintComponent(g, l, rubberStamp, x, 2, d.width, d.height);
  }
}
}}

**コメント [#q1e57b08]
* 参考リンク [#reference]
- [https://community.oracle.com/thread/1392495 Swing - JTabbedPane with non-tabbed text]
- [[JTabbedPaneの余白にJCheckBoxを配置>Swing/TabbedPaneWithCheckBox]]
- [[JTabbedPaneの余白にJButtonを配置>Swing/TabbedPaneWithButton]]
-- `JLabel`ではなくクリック可能な`JButton`を`JTabbedPane`に配置するサンプル

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