Reputation: 3578
Is it possible to get or set the the cell value of a JTable by column name?
Upvotes: 1
Views: 13884
Reputation: 539
you can get a TableColumn from JTable's getColumn method, using the name of the column as the identifier; and get its modelIndex....
but if your table has sorting; then you need to do translation.
I'd recommend implementing what you need in the tableModel for your table.
Upvotes: 0
Reputation: 860
Didn't find a built in method in JTable, but how about this:
private int getColumnByName(JTable table, String name) {
for (int i = 0; i < table.getColumnCount(); ++i)
if (table.getColumnName(i).equals(name))
return i;
return -1;
}
Then you can use the following to set & get cell values :
table.setValueAt(value, rowIndex, getColumnByName(table, colName));
table.getValueAt(rowIndex, getColumnByName(table, colName));
Upvotes: 7