开发者

Change column header in JTable if i know column position

开发者 https://www.devze.com 2023-01-14 02:10 出处:网络
Can I change column header in Jtable tab from \"Name\" to \"Surname\" if I know column position? I want to change column name in second or first tab, not last one.

Can I change column header in Jtable tab from "Name" to "Surname" if I know column position? I want to change column name in second or first tab, not last one.

With this code I can change only co开发者_开发技巧lumn header in last tab. I have 4 tabs.

JTableHeader th = table.getTableHeader();
TableColumnModel tcm = th.getColumnModel();
TableColumn tc = tcm.getColumn(0);
tc.setHeaderValue( "???" );
th.repaint();


You only have to replace your column index by the use of column identifier :

JTableHeader th = table.getTableHeader();
TableColumnModel tcm = th.getColumnModel();
TableColumn tc = tcm.getColumn(tcm.getColumnIndex("Name")); // may not work, see note below
tc.setHeaderValue( "???" );
th.repaint();

The TableColumnModel#getColumnIndex(Object) works with a column identifier. Most of the time, column identifier and column header value are identical. However, in some cases, they can differ (typically when using i18n for column header). But in this case, i guess you have identified your column with a constant id.

0

精彩评论

暂无评论...
验证码 换一张
取 消