Swing/TabWidth のバックアップ(No.19)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/TabWidth へ行く。
- 1 (2005-06-13 (月) 03:13:10)
- 2 (2005-06-13 (月) 05:41:29)
- 3 (2005-07-09 (土) 17:22:59)
- 4 (2005-10-04 (火) 14:54:07)
- 5 (2006-01-06 (金) 14:48:06)
- 6 (2006-02-27 (月) 16:48:13)
- 7 (2006-12-22 (金) 18:56:11)
- 8 (2007-08-30 (木) 14:46:14)
- 9 (2012-08-08 (水) 18:46:10)
- 10 (2013-03-28 (木) 18:01:45)
- 11 (2014-10-26 (日) 15:41:07)
- 12 (2015-11-15 (日) 19:32:18)
- 13 (2017-05-02 (火) 18:32:51)
- 14 (2018-03-01 (木) 14:58:22)
- 15 (2019-08-23 (金) 16:07:47)
- 16 (2021-04-17 (土) 02:58:20)
- 17 (2025-01-03 (金) 08:57:02)
- 18 (2025-01-03 (金) 09:01:23)
- 19 (2025-01-03 (金) 09:02:38)
- 20 (2025-01-03 (金) 09:03:21)
- 21 (2025-01-03 (金) 09:04:02)
- category: swing
folder: TabWidth
title: JTabbedPaneのタブ幅を固定
tags: [JTabbedPane]
author: aterai
pubdate: 2005-06-13T03:13:10+09:00
description: JTabbedPaneのタブ幅をできるだけ一定のサイズに固定します。
image:
Summary
JTabbedPane
のタブ幅をできるだけ一定のサイズに固定します。
Screenshot

Advertisement
Source Code Examples
tabbedPane.setUI(new BasicTabbedPaneUI() {
@Override
protected int calculateTabWidth(int placement, int index, FontMetrics metrics) {
return Math.max(MIN_TAB_WIDTH, super.calculateTabWidth(placement, index, metrics));
}
});
View in GitHub: Java, Kotlin解説
上記のサンプルでは、BasicTabbedPaneUI
のcalculateTabWidth
メソッドをオーバーライドしてJTabbedPane
のタブ幅がタイトル文字列の長さにかかわらず一定以上になるように設定しています。
- タイトル文字列が短くても最低
MIN_TAB_WIDTH(100px)
のタブ幅を維持する - タブランが
2
列以上になると指定されたタブ幅を維持できない場合がある - 以下のように、
html
タグを使ってタブ幅を固定する方法もあるpublic String makeTitle(String title) { return "<html><table width='100'><tr><td align='center'>" + title + "</td></tr></table>"; }