Swing/HeaderRatio のバックアップの現在との差分(No.17)
- category: swing folder: HeaderRatio title: TableColumnの幅を比率で設定 tags: [JTable, JTableHeader, TableColumn, JScrollPane] author: aterai pubdate: 2005-11-28T18:26:47+09:00 description: 列幅調整がデフォルトのJTableで、ヘッダの各TableColumnが指定した比率の幅になるように設定します。 image:
概要
TableColumn
の幅を比率で設定します。
概要
列幅調整がデフォルトのJTable
で、ヘッダの各TableColumn
が指定した比率の幅になるように設定します。
Screenshot
Advertisement
サンプルコード
サンプルコード
private void setTableHeaderColumnRaito() {
int[] list = getWidthRaitoArray();
//System.out.println("a: "+table.getColumnModel().getTotalColumnWidth());
//System.out.println("b: "+table.getSize().width);
int total = table.getSize().width; //table.getColumnModel().getTotalColumnWidth();
int raito = total/getRaitoTotal(list);
for(int i=0;i<table.getColumnModel().getColumnCount()-1;i++) {
TableColumn col = table.getColumnModel().getColumn(i);
int colwidth = list[i]*raito;
col.setPreferredWidth(colwidth); //col.setMaxWidth(colwidth);
TableColumnModel m = table.getColumnModel();
List<Integer> list = getWidthRaitoArray();
int total = table.getSize().width;
double raito = total / (double) getRaitoTotal(list);
for (int i = 0; i < m.getColumnCount() - 1; i++) {
TableColumn col = m.getColumn(i);
int colwidth = (int) (.5 + list.get(i) * raito);
col.setPreferredWidth(colwidth);
total -= colwidth;
}
table.getColumnModel().getColumn(
table.getColumnModel().getColumnCount()-1).setPreferredWidth(total);
//table.getColumnModel().getColumnCount()-1).setMaxWidth(total);
// 最後のカラムで余りを解消
m.getColumn(m.getColumnCount() - 1).setPreferredWidth(total);
table.revalidate();
}
#spandel
#spanend
#spandel
private void setRaito() {
#spanend
int[] list = getWidthRaitoArray();
int total = getTotalColumnWidth(table);
int raito = total/getRaitoTotal(table, list);
for(int i=0;i<table.getColumnModel().getColumnCount()-1;i++) {
TableColumn col = table.getColumnModel().getColumn(i);
int colwidth = list[i]*raito;
col.setMaxWidth(colwidth);
total = total - colwidth;
}
//最後のカラムで誤差を吸収
table.getColumnModel().getColumn(
table.getColumnModel().getColumnCount()-1).setMaxWidth(total);
table.revalidate();
#spandel
}
#spanend
View in GitHub: Java, Kotlin解説
上記のサンプルでは、JTextField
にコロン区切りで入力した比率に従って、ヘッダカラムの幅を調整するようになっています。
解説
上記のサンプルでは、JTextField
にコロン区切りで入力した比率に従って各カラムヘッダの幅を調整しています。
-
ComponentListener#componentResized(...)
がチェックされている場合-
JScrollPane
に追加したComponentListener
で、リサイズされる毎に全ての列幅を設定し直すので、フレームのサイズを変更してもカラムの比率は保持される
-
-
ComponentListener#componentResized(...)
がチェックされていない場合- 列幅調整が
AUTO_RESIZE_SUBSEQUENT_COLUMNS
(デフォルト)なので、フレームをリサイズすると、その幅の変更(デルタ)が、リサイズ可能なすべての列に分散して加算減算される- このため、入力されている比率とは異なる列幅になる
- 列幅調整が
-
ComponentListener#componentResized(...)
がチェックされている場合:-
JScrollPane
に追加したComponentListener
でリサイズが実行されると全ての列幅を設定し直すのでフレームのサイズを変更してもカラムの比率は保持される
-
-
ComponentListener#componentResized(...)
がチェックされていない場合:- 列幅調整が
AUTO_RESIZE_SUBSEQUENT_COLUMNS
(デフォルト)なので、フレームをリサイズするとその幅の変更(デルタ)がリサイズ可能なすべての列に分散して加算減算される- このため入力されている比率とは異なる列幅になる
-
TableColumn#setMaxWidth
メソッドでカラムの幅を指定する場合、マウスのドラッグによるリサイズは不可
- 列幅調整が
- -
TableColumn#setMaxWidth
メソッドでカラムの幅を指定する場合は、マウスのドラッグによるサイズの変更はできません。