Java – sort double values in JTable
I found many problems related to this, but I didn't find a simple way to solve my problem
I can't find a way to make my JTable sort the correct double value
I extend abstracttablemodel to receive a class array and return the correct type of each 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];
}
...
Then in my custom JTable constructor:
TableRowSorter<TableModelMod> sorter = new TableRowSorter<TableModelMod>((TableModelMod)getModel());
However, I encountered this error when adding rows:
java.lang.IllegalArgumentException: Cannot format given Object as a Number
It is in the method decimalformat Format (object number, StringBuffer, toappendto, fieldposition POS) fails. It accepts most number types, but double
If I use another class for the double column, I have no errors, but the sorting still doesn't work as expected I've tried different number classes, but I don't seem to sort doubles 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 whether I need to implement custom rowsorter, custom cellrenderer or both
Can someone guide me how to solve this simpler method?
Thank you very much and best regards
Editing: resolved
It's very embarrassing to tell where the problem is
The ArrayList containing the object [] row is populated from the database resultset using getString (int) instead of GetObject (int) or getdouble (int), so the renderer cannot use this value as a double Strangely, it does not use integer or number as the column class to provide exceptions, but it is still sorted as string I look for problems in the wrong class because I'm sure my ArrayList contains only objects
Thank you very much for your example. Looking at them, I noticed that my doubles are actually strings, and then I can find where the conversion takes place
Solution
Look at this code It sorts double values
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
public class RowSorterDemo {
public static void main(String args[]) {
JFrame frame = new JFrame("Sort Table Demo");
frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
Object rows[][] = { { "J",23.1 },{ "R",21.1,},{ "E",21.2,{ "B",27.1,{ "A",25.2,{ "S",22.9,};
String columns[] = { "Name","Age" };
TableModel model = new DefaultTableModel(rows,columns) {
public Class getColumnClass(int column) {
Class returnValue;
if ((column >= 0) && (column < getColumnCount())) {
returnValue = getValueAt(0,column).getClass();
} else {
returnValue = Object.class;
}
return returnValue;
}
};
JTable table = new JTable(model);
RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
table.setRowSorter(sorter);
JScrollPane pane = new JScrollPane(table);
frame.add(pane,BorderLayout.CENTER);
frame.setSize(300,150);
frame.setVisible(true);
}
}
I slightly modified the source given by this link, so it needs to double the value
