I have a JTable, and i want to sort rows sometimes by integer (size column), sometimes by String (file path).
So i searched it on google and i’ve arrived here. i’ve known that i’ve to override a method of DefaultTableModel, called getColumnClass. So i link here my code.
class Personal_model extends DefaultTableModel{
Personal_model(String[][] s,String[] i){
super(s,i);
}
@Override
public Class<?> getColumnClass(int columnIndex){
if (columnIndex!=2)
return String.class;
else
return Integer.class;
}
}
And here the code to create the table, by the model ‘Personal_model’; i also set rowsorter.
BUT ALL THIS DOESN’T WORK!!!!! help me pls
modeltable = new Personal_model(data,col);
table = new JTable(modeltable);
table.setRowSorter(new TableRowSorter<Personal_model>(modeltable));
Normally, without my sorter, all is perfeclty visualized, and Strings are sorted correctly (it’s obvious, beacuse normally it’s all sorted by String..)
1) please read tutorial about JTable that’s contains TableRowSorter example, issue about RowSorter must be in your code
2) by default you can to use follows definition for ColumnClass,
3) or you can to hardcode that
4) or override
RowSorter(notice crazy code)