Swing/TabTitleAlignment のバックアップの現在との差分(No.1)
TITLE:JTabbedPaneのTabTitleを左揃えに変更
Posted by terai at 2010-10-11
JTabbedPaneのTabTitleを左揃えに変更
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:
概要
JTabbedPane
のTabTitle
の揃えをデフォルトの中央揃えから左揃えに変更します。
- &jnlp;
- &jar;
- &zip;
Screenshot
Advertisement
#screenshot
サンプルコード
#spanend
#spandel
class MyTabbedPaneUI extends javax.swing.plaf.metal.MetalTabbedPaneUI {
#spanend
#spanadd
* サンプルコード [#sourcecode]
#spanend
#spanadd
#code(link){{
#spanend
#spanadd
class MyTabbedPaneUI extends MetalTabbedPaneUI {
#spanend
@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;
// ...
}
}
解説
- 上
- デフォルト(中央揃え)
- 中
- WindowsTabbedPaneUI#layoutLabel(...)などをオーバーライドして左揃えに変更
- 下
- JTabbedPaneのタブを等幅にしてタイトルをクリップ、ButtonTabComponent.javaを変更してタイトルを左揃え、TabButtonを右揃え
解説
- 上
- デフォルト(中央揃え)
- 中
-
WindowsTabbedPaneUI#layoutLabel(...)
などをオーバーライドして左揃えに変更
-
- 下
- JTabbedPaneのタブを等幅にしてタイトルをクリップ、ButtonTabComponent.javaを変更してタイトルを左揃え、
TabButton
(閉じる)を右揃え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, BorderLayout.EAST); // ...
- JTabbedPaneのタブを等幅にしてタイトルをクリップ、ButtonTabComponent.javaを変更してタイトルを左揃え、
参考リンク
- JTabbedPaneのタブを等幅にしてタイトルをクリップ
- ButtonTabComponent.java
- OTN Discussion Forums : JTabbedPane title alignment
参考リンク
- JTabbedPaneのタブを等幅にしてタイトルをクリップ
- ButtonTabComponent.java
- Swing - JTabbedPane title alignment
- Bug ID: 4220177 labels within JTabbedPane tabs should be alignable