Swing/TabTitleAlignment のバックアップ(No.21)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/TabTitleAlignment へ行く。
  - 1 (2010-10-11 (月) 18:19:33)
- 2 (2012-12-26 (水) 06:41:05)
- 3 (2013-09-03 (火) 01:30:18)
- 4 (2014-11-01 (土) 00:46:09)
- 5 (2014-11-25 (火) 03:03:31)
- 6 (2015-03-20 (金) 15:25:10)
- 7 (2017-02-16 (木) 21:19:52)
- 8 (2017-04-04 (火) 14:17:08)
- 9 (2017-11-02 (木) 15:34:40)
- 10 (2017-12-27 (水) 18:31:19)
- 11 (2019-05-22 (水) 19:35:38)
- 12 (2019-12-20 (金) 14:55:38)
- 13 (2021-06-22 (火) 17:54:35)
- 14 (2022-08-20 (土) 22:15:25)
- 15 (2025-01-03 (金) 08:57:02)
- 16 (2025-01-03 (金) 09:01:23)
- 17 (2025-01-03 (金) 09:02:38)
- 18 (2025-01-03 (金) 09:03:21)
- 19 (2025-01-03 (金) 09:04:02)
- 20 (2025-06-19 (木) 12:41:37)
- 21 (2025-06-19 (木) 12:43:47)
 
- 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:  
Summary
JTabbedPaneのTabTitleの揃えをデフォルトの中央揃えから左揃えに変更します。
Screenshot

Advertisement
Source Code Examples
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;
    // ...
  }
}
Description
- 上
- デフォルト(中央揃え)
 
- 中
- WindowsTabbedPaneUI#layoutLabel(...)などをオーバーライドして左揃えに変更
 
- 下
- JTabbedPaneのタブを等幅にしてタイトルをクリップ、ButtonTabComponent.javaを変更してタイトルを左揃え、TabButton(閉じる)を右揃えpublic ButtonTabComponent(final JTabbedPane pane) { // 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を変更してタイトルを左揃え、
Reference
- JTabbedPaneのタブを等幅にしてタイトルをクリップ
- ButtonTabComponent.java
- Swing - JTabbedPane title alignment
- Bug ID: 4220177 labels within JTabbedPane tabs should be alignable