I found many questions related to this, but I did not find a simple solution to my problem.
I cannot find a way to correctly execute double JTable values.
I extended AbstractTableModel to get an array of the class and return the corresponding types for the column:
class TableModelMod extends AbstractTableModel{ private ArrayList data; private String [] headers; private Class [] types; TableModelMod(String [] heads, ArrayList datas, Class [] classes){ headers = heads; data = datas; types = classes; } ... @Override public Class getColumnClass(int c){ if (c > types.length - 1) return null; else return types[c]; } ...
And then in my custom JTable constructor:
TableRowSorter<TableModelMod> sorter = new TableRowSorter<TableModelMod>((TableModelMod)getModel());
But then I get this error when adding lines:
java.lang.IllegalArgumentException: Cannot format given Object as a Number
Cannot execute the DecimalFormat.format(Object number, StringBuffer toAppendTo, FieldPosition pos) , which accepts most numeric types, but Double.
If I use another class for double columns, I do not get an error, but sorting does not work properly. I tried with different number classes, but no one seems to sort the binary letters correctly:
@Override public Class getColumnClass(int c){ if (c > types.length - 1) return null; else if (types[c] == Double.class) return Number.class; else return types[c]; }
I'm not sure if I need to implement a custom RowSorter, a custom CellRenderer, or both.
Can someone guide me on how to fix this in a simpler way?
Many thanks and best regards.
CONNECTED: SOLVED
Itβs hard to say where the problem was.
An ArrayList containing the Object [] strings was populated from the ResultSet database using getString (int) instead of getObject (int) or getDouble (int), so the value could not be used as a Double handler. It is strange that he made no exceptions using Integer or Number as the column class, but in any case it was sorted as String. I searched for the problem in the wrong classes, as I was convinced that my ArrayList contains only objects.
Thank you very much for your examples, looking at them, I noticed that my doubles were actually strings, and then I could find where the transformation happened.