Technology

JTable便利帳

JTableで自分が結構使う設定などのメモ。

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
// テーブルを同時に1列(1セル)しか選択できないようにする
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 
// テーブルの編集を禁止する
table.setDefaultEditor(Object.class, null);
 
// ヘッダをクリックしてソートできるようにする
table.setAutoCreateRowSorter(true);
 
// カラムの移動を禁止する
table.getTableHeader().setReorderingAllowed(false);
 
// ソートした後など、最初のテーブルのデータの並び方と異なっている場合に、値を取得する
int modelRow = table.convertRowIndexToModel(table.getSelectedRow());
Object value = model.getValueAt(modelRow, 0);
 
// デフォルトでソートする
table.getRowSorter().setSortKeys(Arrays.asList(new RowSorter.SortKey(0, SortOrder.ASCENDING)));

Topic