Swing/TabWidth のバックアップソース(No.16)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - 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)
--- category: swing folder: TabWidth title: JTabbedPaneのタブ幅を固定 tags: [JTabbedPane] author: aterai pubdate: 2005-06-13T03:13:10+09:00 description: JTabbedPaneのタブ幅をできるだけ一定のサイズに固定します。 image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTVAYay_fI/AAAAAAAAAm8/1gWXosPj93A/s800/TabWidth.png --- * 概要 [#summary] `JTabbedPane`のタブ幅をできるだけ一定のサイズに固定します。 #download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTVAYay_fI/AAAAAAAAAm8/1gWXosPj93A/s800/TabWidth.png) * サンプルコード [#sourcecode] #code(link){{ 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)); } }); }} * 解説 [#explanation] 上記のサンプルでは、`BasicTabbedPaneUI`の`calculateTabWidth`メソッドをオーバーライドして`JTabbedPane`のタブ幅がタイトル文字列の長さにかかわらず一定以上になるように設定しています。 - タイトル文字列が短くても、最低`MIN_TAB_WIDTH(100px)`のタブ幅を維持する - タブランが`2`列以上になると、指定されたタブ幅を維持できない場合がある ---- 以下のように、`html`タグを使ってタブ幅を固定する方法もあります。 #code{{ public String makeTitle(String title) { return "<html><table width='100'><tr><td align='center'>" + title + "</td></tr></table>"; } }} * 参考リンク [#reference] - [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/plaf/basic/BasicTabbedPaneUI.html#calculateTabWidth-int-int-java.awt.FontMetrics- BasicTabbedPaneUI#calculateTabWidth(...) (Java Platform SE 8)] * コメント [#comment] #comment #comment