I have read several questions about JTable sorting:
Problems with JTable sorting of integer values
JTable + Sorting specific field
but I still can't manage to get it right, for some reason.
I have written this:
String allItemsColumns [] = { "#", "Name", "Stock",
"Price", "Type", "Supplier", "Location", "" };
allItemsTableModel = new DefaultTableModel(null, allItemsColumns);
allItemsTable = new JTable(allItemsTableModel)
{
Class<?>[] types = { Integer.class, String.class, Integer.class,
Double.class, String.class, String.class, String.class, ImageIcon.class };
@Override
public Class<?> getColumnClass(int columnIndex) {
return this.types[columnIndex];
}
};
but I still get
0
10000
20
when I sort stock. Probably it's an obvious one, but I'm really missing it at the moment.
I do not think it matters how I add info as I (think so that) tell it to read the columns as Integers, Doubles or Strings
My sorting method:
allItemsTable.setAutoCreateRowSorter(true);
TableRowSorter<DefaultTableModel> rowSorter =
(TableRowSorter<DefaultTableModel>)allItemsTable.getRowSorter();
rowSorter.setComparator(3, new Comparator<String>() {
@Override
public int compare(String o1, String o2)
{
return Integer.parseInt(o1) - Integer.parseInt(o2);
}
});
I have taken it from one of the questions I saw. Column #3 is "Stock" which is Integers only, but the result, like I said is:
0
10000
20