I have a JTable that uses a DefaultTableModel and I allow for sorting when the user clicks on the column headers. However, when the user clicks on a header for a column that has data of type integer it does not sort properly. It seems like it is sorting by String instead of an integer type.
Here is the part of my code where I actually add the data to the table:
DefaultTableModel aModel = (DefaultTableModel) mainView.logEntryTable.getModel();
ResultSetMetaData rsmd; try {
mainView.logEntriesTableModel.setRowCount(0);
rsmd = rs.getMetaData();
int colNo = rsmd.getColumnCount();
while(rs.next()){
Object[] objects = new Object[colNo];
for(int i=0;i<colNo;i++){
objects[i]=rs.getObject(i+1);
}
aModel.addRow(objects);
count++;
}
mainView.logEntryTable.setModel(aModel);
mainView.logEntryTable.getColumnModel().getColumn(0).setMaxWidth(80);
So I tried to override that method and ended up with this:
@Override
public Class<?> getColumnClass(int columnIndex){
if( columnIndex == 0){
// Return the column class for the integer column
}else{
// Return the column class like we normally would have if we didn't override this method
}
return null;
}
};
I've never overridden this before and I'm not quite sure what it is expecting me to do here.