如果你的JTable数据是根据Vector构成的,可以通过在单击JTable列头的事件中实现,方法如下: public class VectorSort implements Comparator { public static int compare_row = 0;//比较的列 public static boolean desc = false;//升降序 public static boolean ingore = false;//大小写
public VectorSort() { } public int compare(Object o1, Object o2){ Vector vrow1 = (Vector) o1; Vector vrow2 = (Vector) o2; if(compare_row+1>vrow1.size()){ //System.out.println("无效的比较"); return 0; } String str1 =(String) vrow1.elementAt(compare_row); String str2 =(String) vrow2.elementAt(compare_row); if(str1==null) str1 = ""; if(str2==null) str2 = ""; int return_int = 0; if (desc) return_int = str2.compareTo(str1); if (ingore) return_int = str1.toLowerCase().compareTo(str2.toLowerCase()); if (ingore&&desc) return_int = str2.toLowerCase().compareTo(str1.toLowerCase()); if (!(ingore&&desc)) return_int = str1.compareTo(str2); return return_int; } } 调用: Collections.sort(yourJTableData, new VectorSort());
public class VectorSort implements Comparator { public static int compare_row = 0;//比较的列
public static boolean desc = false;//升降序
public static boolean ingore = false;//大小写
public VectorSort() {
} public int compare(Object o1, Object o2){
Vector vrow1 = (Vector) o1;
Vector vrow2 = (Vector) o2;
if(compare_row+1>vrow1.size()){
//System.out.println("无效的比较");
return 0;
}
String str1 =(String) vrow1.elementAt(compare_row);
String str2 =(String) vrow2.elementAt(compare_row);
if(str1==null)
str1 = "";
if(str2==null)
str2 = "";
int return_int = 0;
if (desc)
return_int = str2.compareTo(str1);
if (ingore)
return_int = str1.toLowerCase().compareTo(str2.toLowerCase());
if (ingore&&desc)
return_int = str2.toLowerCase().compareTo(str1.toLowerCase());
if (!(ingore&&desc))
return_int = str1.compareTo(str2);
return return_int;
}
}
调用:
Collections.sort(yourJTableData, new VectorSort());