• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JTabbedPaneの余白にJButtonを配置
#navi(../)
*JTabbedPaneの余白にJButtonを配置 [#g997f976]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2008-03-03~
更新日:&lastmod;

#contents

**概要 [#da23e70c]
JTabbedPaneのタブエリアに余白を作成し、そこにOverlayLayoutを使ってJButtonを配置します。

#screenshot

**サンプルコード [#l9d78f5a]
#code{{
JPanel p = new JPanel();
p.setLayout(new OverlayLayout(p));
button.setAlignmentX(0.0f);
button.setAlignmentY(0.0f);
tabPane.setAlignmentX(0.0f);
tabPane.setAlignmentY(0.0f);
p.add(button);
p.add(tabPane);
}}

#code{{
public InsetsUIResource getButtonPaddingTabAreaInsets(JButton b) {
  int bw = b.getPreferredSize().width;
  int bh = b.getPreferredSize().height;
  Insets insets = UIManager.getInsets("TabbedPane.tabInsets");
  Insets ti = (insets!=null)?insets:new Insets(0,0,0,0);
  insets = UIManager.getInsets("TabbedPane.tabAreaInsets");
  Insets ai = (insets!=null)?insets:new Insets(0,0,0,0);
  FontMetrics metrics = getFontMetrics(getFont());
  int tih = bh - metrics.getHeight()-ti.top-ti.bottom-ai.bottom;
  return new InsetsUIResource(Math.max(ai.top, tih), bw+ai.left, ai.bottom, ai.right);
}
}}
-&jnlp;
-&jar;
-&zip;

**解説 [#f485c989]
上記のサンプルは、以下のような動作の仕様になっています。
-タブエリアの左上にあるボタンをクリックするとタブが追加される(FireFox風?)
上記のサンプルは、以下のような動作((OperaやFireFoxなどのタブブラウザ風?))の仕様になっています。
-タブエリアの左上にあるボタンをクリックするとタブが追加される
-メニューからすべてのタブを削除する
-タブエリアに余裕がある場合は80pt、無い場合は(タブエリアの幅/タブ数)と、常にタブ幅は一定(Opera風?)
-タブエリアに余裕がある場合は80pt、無い場合は(タブエリアの幅/タブ数)と、常にタブ幅は一定
--折り返しや、スクロールが発生するとレイアウトが崩れるため

コンポーネントの追加には、以下の方法を使用しています(比較:[[JTabbedPaneの余白にJCheckBoxを配置>Swing/TabbedPaneWithCheckBox]])。
-ボタンの幅だけ、tabAreaInsets の左余白を拡大する
--UIManager.getInsets("TabbedPane.tabAreaInsets")などを使用するため、Synthなど(GTK, Nimbus)のLnFには対応していない
--[[Nimbus L&F: java.lang.NullPointer Exception throws when extended BaseUI Components>http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6634504]]
-OverlayLayout で、JButtonとJTabbedPane(上で作った余白に)を重ねて表示
--このため、JTabbedPane.TOP にしか対応していない

**参考リンク [#j34dcca9]
-[[famfamfam.com: Mini Icons>http://www.famfamfam.com/lab/icons/mini/]]
--アイコン
-[[OverlayLayoutの使用>Swing/OverlayLayout]]
-[[JTabbedPaneの余白にJCheckBoxを配置>Swing/TabbedPaneWithCheckBox]]
-[[JTabbedPaneのタイトルをクリップ>Swing/ClippedTabLabel]]

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