I have a JTable
with 3 columns. Each column has its own format and the table looks like this:
The problem is (as you can see) that the sorting is incorrect (setAutoCreateRowSorter
).
I tried to define my own Object type for col3
that implements Comparable
and also have a toString()
method for this Object. But it seems like this doesn't help me to get the sorting correct.
Any idea what I am doing wrong?
public class SortJTable {
public static void main(String[] args) {
String[] columns = getTableColumns();
Object[][] tableData = getTableValues();
TableModel model = new DefaultTableModel(tableData, columns) {
@Override
public Class getColumnClass(int col) {
if (col == 2) // third column is a TablePercentValue
return TablePercentValue.class;
else
return String.class;
}
};
JTable table = new JTable(model);
table.setAutoCreateRowSorter(true); // Make it possible to column-sort
JFrame frame = new JFrame();
frame.add(new JScrollPane(table));
frame.pack();
frame.setVisible(true);
}
private static String[] getTableColumns(){
String[] columns = new String[3];
columns[0] = "col1";
columns[1] = "col2";
columns[2] = "col3";
return columns;
}
private static Object[][] getTableValues(){
Object[][] tableData = new Object[100][3];
for(int i=0; i<tableData.length; i++){
for(int j=0; j<tableData[0].length; j++){
String value;
if(j==2)
value = i+","+j+"%";
else if(j == 1)
value = i+":"+j;
else
value = i+""+j;
tableData[i][j] = value;
}
}
return tableData;
}
}
class TablePercentValue implements Comparable<TablePercentValue> {
private String value;
private double compValue;
public TablePercentValue(String value){
this.value = value;
// Remove "%"-sign and convert to double value
compValue = Double.parseDouble(value.replace("%", ""));
}
public String toString(){
return value;
}
@Override
public int compareTo(TablePercentValue o) {
return compValue>o.compValue ? 1 : -1;
}
}
Your overridden getColumnClass
is lying: the second column is not of type TablePercentValue
, it's still a String
. For the purposes of your example, this can be fixed where you populate the data:
for(int j=0; j<tableData[0].length; j++){
if(j==2)
tableData[i][j] = new TablePercentValue(i+","+j+"%");
else if(j == 1)
tableData[i][j] = i+":"+j;
else
tableData[i][j] = i+""+j;
tableData[i][j] = value;
}
Inside the TablePercentValue
constructor I had to add an extra replace(",", ".")
compValue = Double.parseDouble(value.replace("%", "").replace(",", "."));
but this might just be a localization thing and run fine for you.