• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JTabbedPaneのTabTitleを左揃えに変更
#navi(../)
#tags(JTabbedPane, Alignment, JButton)
RIGHT:Posted by &author(aterai); at 2010-10-11
*JTabbedPaneのTabTitleを左揃えに変更 [#y839b018]
``JTabbedPane``の``TabTitle``をデフォルトに中央揃えから左揃えに変更します。
---
category: swing
folder: TabTitleAlignment
title: JTabbedPaneのTabTitleを左揃えに変更
tags: [JTabbedPane, Alignment, JButton]
author: aterai
pubdate: 2010-10-11T18:19:33+09:00
description: JTabbedPaneのTabTitleの揃えをデフォルトの中央揃えから左揃えに変更します。
image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTU2Jp4a6I/AAAAAAAAAms/x6g2ML8eyyQ/s800/TabTitleAlignment.png
---
* 概要 [#summary]
`JTabbedPane`の`TabTitle`の揃えをデフォルトの中央揃えから左揃えに変更します。

-&jnlp;
-&jar;
-&zip;
#download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTU2Jp4a6I/AAAAAAAAAms/x6g2ML8eyyQ/s800/TabTitleAlignment.png)

//#screenshot
#ref(http://lh5.ggpht.com/_9Z4BYR88imo/TQTU2Jp4a6I/AAAAAAAAAms/x6g2ML8eyyQ/s800/TabTitleAlignment.png)

**サンプルコード [#a4171eff]
* サンプルコード [#sourcecode]
#code(link){{
class MyTabbedPaneUI extends javax.swing.plaf.metal.MetalTabbedPaneUI {
class MyTabbedPaneUI extends MetalTabbedPaneUI {
  @Override protected void layoutLabel(int tabPlacement,
                                       FontMetrics metrics, int tabIndex,
                                       String title, Icon icon,
                                       Rectangle tabRect, Rectangle iconRect,
                                       Rectangle textRect, boolean isSelected ) {
    textRect.x = textRect.y = iconRect.x = iconRect.y = 0;
    //...
    // ...
    SwingUtilities.layoutCompoundLabel((JComponent) tabPane,
                                       metrics, title, icon,
                                       SwingUtilities.CENTER,
                                       SwingUtilities.LEFT, //CENTER, <----
                                       SwingUtilities.CENTER,
                                       SwingUtilities.TRAILING,
                                       tabRect,
                                       iconRect,
                                       textRect,
                                       textIconGap);
    tabPane.putClientProperty("html", null);
    textRect.translate(tabInsets.left, 0); //<----
    textRect.width -= tabInsets.left+tabInsets.right;
    //...
    textRect.width -= tabInsets.left + tabInsets.right;
    // ...
  }
}
}}

**解説 [#m7c4ecf6]
-上
--デフォルト(中央揃え)
-中
--``WindowsTabbedPaneUI#layoutLabel(...)``などをオーバーライドして左揃えに変更
-下
--[[JTabbedPaneのタブを等幅にしてタイトルをクリップ>Swing/ClippedTitleTab]]、[http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TabComponentsDemoProject/src/components/ButtonTabComponent.java ButtonTabComponent.java]を変更してタイトルを左揃え、``TabButton``(閉じる)を右揃え

* 解説 [#explanation]
- 上
-- デフォルト(中央揃え)
- 中
-- `WindowsTabbedPaneUI#layoutLabel(...)`などをオーバーライドして左揃えに変更
- 下
-- [[JTabbedPaneのタブを等幅にしてタイトルをクリップ>Swing/ClippedTitleTab]]、[https://docs.oracle.com/javase/tutorial/uiswing/examples/components/TabComponentsDemoProject/src/components/ButtonTabComponent.java ButtonTabComponent.java]を変更してタイトルを左揃え、`TabButton`(閉じる)を右揃え
#code{{
public ButtonTabComponent(final JTabbedPane pane) {
  //unset default FlowLayout' gaps
  //super(new FlowLayout(FlowLayout.LEFT, 0, 0));
  // unset default FlowLayout' gaps
  // super(new FlowLayout(FlowLayout.LEFT, 0, 0));
  super(new BorderLayout(0, 0));
  //...
  //add(button);
  // ...
  // add(button);
  add(button, BorderLayout.EAST);
  //...
  // ...
}}

**参考リンク [#vd528ee3]
-[[JTabbedPaneのタブを等幅にしてタイトルをクリップ>Swing/ClippedTitleTab]]
-[http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TabComponentsDemoProject/src/components/ButtonTabComponent.java ButtonTabComponent.java]
-[http://forums.oracle.com/forums/thread.jspa?threadID=1554158 OTN Discussion Forums : JTabbedPane title alignment]
-[http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4220177 Bug ID: 4220177 labels within JTabbedPane tabs should be alignable]
* 参考リンク [#reference]
- [[JTabbedPaneのタブを等幅にしてタイトルをクリップ>Swing/ClippedTitleTab]]
- [https://docs.oracle.com/javase/tutorial/uiswing/examples/components/TabComponentsDemoProject/src/components/ButtonTabComponent.java ButtonTabComponent.java]
- [https://community.oracle.com/thread/1556158 Swing - JTabbedPane title alignment]
- [https://bugs.openjdk.org/browse/JDK-4220177 Bug ID: 4220177 labels within JTabbedPane tabs should be alignable]

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