当我点击列标题以按升序排序时,如何在JTable中的空单元格显示在具有值的单元格下方。现在,空单元格出现在单元格上方,其中包含数字1和2。
TableRowSorter<TableModel> tableRowSorter = new TableRowSorter<TableModel>(spreadsheetModel)
{
Comparator<Object> comparator = new Comparator<Object>()
{
public int compare(Object o1, Object o2)
{
try
{
Double d1 = Double.parseDouble(o1.toString());
Double d2 = Double.parseDouble(o2.toString());
return d1.compareTo(d2);
}
catch (NumberFormatException numberFormatException)
{
try
{
Integer i1 = Integer.parseInt(o1.toString());
Integer i2 = Integer.parseInt(o2.toString());
return i1.compareTo(i2);
}
catch (Exception e)
{
return o1.toString().compareTo(o2.toString());
}
}
}
};
public Comparator getComparator(int column)
{
return comparator;
};
};
for(int i = 0; i < table.getColumnCount(); i++)
tableRowSorter.setComparator(i, tableRowSorter.getComparator(i));
tableRowSorter.setSortKeys(null);
table.setRowSorter(tableRowSorter);
table.setAutoCreateRowSorter(false);
答案 0 :(得分:1)
在进行比较之前,您可以检查空值。类似的东西:
// Handle sorting of null values
if (o1 == null && o2 == null) return 0;
if (o1 == null) return 1;
if (o2 == null) return -1;