dポイントプレゼントキャンペーン実施中!

データベースからデータを取得して(ResultSetMetaData)、Jtableに表示したのですが、列幅を項目ごとに変更したいのですが、できるのでしょうか? 項目ヘッダーはできたのですが。。。

ResultSetMetaData rm = rs.getMetaData();
int cnum = rm.getColumnCount();
colname = new ArrayList<String>(cnum);
//列名の取得
String[] hder = {"月日","区分","コード","商品"};
for(int i=1; i<=cnum; i++){
colname.add(hder[i-1]);
}
//行の取得
data = new ArrayList<ArrayList>();
while(rs.next()){
ArrayList<String> rowdata = new ArrayList<String>();
for(int i=1; i<=cnum; i++){
if (rs.getObject(i)==null){
rowdata.add("");}
else{
rowdata.add(rs.getObject(i).toString());}

}
data.add(rowdata);
}
表示はできるのですが、各項目ごとに列幅が、しようとMaxsizeとかsetPreferredWidth などを行ってもうまくでいきません。

A 回答 (1件)

私の記憶が確かならこんな感じで、、、





public void initialyzeJTable(JTable jtable){


jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

TableColumnModel tcm = jtable.getColumnModel();
initialyzeTableColumn(tcm, 0, 50);
initialyzeTableColumn(tcm, 1, 200);
initialyzeTableColumn(tcm, 2, 80);
initialyzeTableColumn(tcm, 3, 300);


}

private void initialyzeTableColumn(TableColumnModel tcm, int columnIndex, int width){

TableColumn tc = tcm.getColumn(columnIndex);

tc.setResizable(true);
tc.setPreferredWidth(size);


}


必要であればレンダラーなども一緒に設定してしまうのがいいかもしれません。
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!