2013-03-01 67 views
2

在另一個線程中,我發現這個比較器(帖子的底部)用於對可以由整數,字符串或兩者組成的JTable列進行排序。我無法弄清楚如何將它應用到我的JTable。我的表格之前使用了自動創建的行分類器。我設置爲false,我現在使用:將列比較器設置爲JTable時遇到問題

TableRowSorter<MyTableModel> rowSorter = new TableRowSorter<MyTableModel>(); 
jtable.setRowSorter(rowSorter); 
rowSorter.setComparator(0, c1); 

我得到一個索引越界異常說我提供了一個無效範圍。我的表格有多個列。這是應用比較器的正確方法嗎?我覺得這不是做到這一點的方法。

Comparator c1 = new java.util.Comparator() { 
    /** 
    * Custom compare to sort numbers as numbers. 
    * Strings as strings, with numbers ordered before strings. 
    * 
    * @param o1 
    * @param o2 
    * @return 
    */ 
@Override 
      public int compare(Object oo1, Object oo2) { 
       boolean isFirstNumeric, isSecondNumeric; 
       String o1 = oo1.toString(), o2 = oo2.toString(); 


     isFirstNumeric = o1.matches("\\d+"); 
     isSecondNumeric = o2.matches("\\d+"); 

     if (isFirstNumeric) { 
      if (isSecondNumeric) { 
       return Integer.valueOf(o1).compareTo(Integer.valueOf(o2)); 
      } else { 
       return -1; // numbers always smaller than letters 
      } 
     } else { 
      if (isSecondNumeric) { 
       return 1; // numbers always smaller than letters 
      } else { 
       isFirstNumeric = o1.split("[^0-9]")[0].matches("\\d+"); 
       isSecondNumeric = o2.split("[^0-9]")[0].matches("\\d+"); 

       if (isFirstNumeric) { 
        if (isSecondNumeric) { 
         int intCompare = Integer.valueOf(o1.split("[^0-9]")[0]).compareTo(Integer.valueOf(o2.split("[^0-9]")[0])); 
         if (intCompare == 0) { 
          return o1.compareToIgnoreCase(o2); 
         } 
         return intCompare; 
        } else { 
         return -1; // numbers always smaller than letters 
        } 
       } else { 
        if (isSecondNumeric) { 
         return 1; // numbers always smaller than letters 
        } else { 
         return o1.compareToIgnoreCase(o2); 
        } 
       } 
      } 
     } 
    } 
}; 
+0

爲了更好地幫助越早,張貼[SSCCE(http://sscce.org/)。 – 2013-03-01 16:37:49

回答

7

手動設置RowSorter的時候,你必須要與模型自己保持它同步的護理:

TableRowSorter sorter = new TableRowSorter(); 
table.setRowSorter(sorter); 
sorter.setModel(table.getModel()); 
sorter.setComparator(myComparator); 
0

@kleopatra,您可能沒有一個模型,當你從獲取數據例如原始文本文件,如.csv。因此,所有的列都是字符串,而其中有合法的數字,所以您想要將這些列作爲數字進行排序,而不是字符串(因此避免着名的數字1( 200 ...)。

謝謝user1202394找到了這個其他線程,你能提供給我們鏈接嗎?

我設法使其工作與三個新的代碼部分預期:

Comparator myComparator = new java.util.Comparator() { 
    /** 
    * Custom compare to sort numbers as numbers. 
    * Strings as strings, with numbers ordered before strings. 
    * 
    * @param o1 
    * @param o2 
    * @return 
    */ 
    @Override 
    public int compare(Object oo1, Object oo2) { 
     boolean isFirstNumeric, isSecondNumeric; 
     String o1 = oo1.toString(), o2 = oo2.toString(); 

     isFirstNumeric = o1.matches("\\d+"); 
     isSecondNumeric = o2.matches("\\d+"); 

     if (isFirstNumeric) { 
      if (isSecondNumeric) { 
       return Integer.valueOf(o1).compareTo(Integer.valueOf(o2)); 
      } else { 
       return -1; // numbers always smaller than letters 
      } 
     } else { 
      if (isSecondNumeric) { 
       return 1; // numbers always smaller than letters 
      } else { 
       // Those lines throw ArrayIndexOutOfBoundsException 
       //      isFirstNumeric = o1.split("[^0-9]")[0].matches("\\d+"); 
       //      isSecondNumeric = o2.split("[^0-9]")[0].matches("\\d+"); 

       // Trying to parse String to Integer. 
       // If there is no Exception then Object is numeric, else it's not. 
       try{ 
        Integer.parseInt(o1); 
        isFirstNumeric = true; 
       }catch(NumberFormatException e){ 
        isFirstNumeric = false; 
       } 
       try{ 
        Integer.parseInt(o2); 
        isSecondNumeric = true; 
       }catch(NumberFormatException e){ 
        isSecondNumeric = false; 
       } 

       if (isFirstNumeric) { 
        if (isSecondNumeric) { 
         int intCompare = Integer.valueOf(o1.split("[^0-9]")[0]).compareTo(Integer.valueOf(o2.split("[^0-9]")[0])); 
         if (intCompare == 0) { 
          return o1.compareToIgnoreCase(o2); 
         } 
         return intCompare; 
        } else { 
         return -1; // numbers always smaller than letters 
        } 
       } else { 
        if (isSecondNumeric) { 
         return 1; // numbers always smaller than letters 
        } else { 
         return o1.compareToIgnoreCase(o2); 
        } 
       } 
      } 
     } 
    } 
}; 

TableRowSorter sorter = new TableRowSorter(); 
table.setRowSorter(sorter); 
sorter.setModel(table.getModel()); 
sorter.setComparator(myComparator); 

// Apply Comparator to all columns 
for(int i = 0 ; i < table.getColumnCount() ; i++) 
    rowSorter.setComparator(i, c1);