TableColumnの幅を比率で設定

編集者:Terai Atsuhiro
作成日:2005-11-28
更新日:2021-11-27 (土) 13:30:44

概要

TableColumnの幅を比率で設定します。

http://terai.xrea.jp/swing/headerratio/screenshot.png

サンプルコード

TableColumn col0 = table.getColumnModel().getColumn(0);
TableColumn col1 = table.getColumnModel().getColumn(1);
TableColumn col2 = table.getColumnModel().getColumn(2);
int w = table.getBounds(null).width;
if(w==0) w = 512;
int wr = list[0]+list[1]+list[2];
col0.setMaxWidth(list[0]*w/wr);
col1.setMaxWidth(list[1]*w/wr);
col2.setMaxWidth(list[2]*w/wr);
table.revalidate();

解説

上記のサンプルでは、カンマ区切りで入力した比率にヘッダカラムの幅を調整するようになっています。

JTable#setAutoResizeMode(JTable.AUTO_RESIZE_OFF)で、フレームサイズ固定なので、カラムの幅の指定にはTableColumn#setMaxWidthメソッドを使用しています。

コメント