简体   繁体   中英

JTable RowSorter after performing operations with values of cells

I have cells with which I have to perform some operations before sorting the rows. For Example: If a string is given, I convert it to the sum of its ASCII values. And then I want to sort the JTable as per the sums of the ASCII values. The code below is for sorting alphabetically. But I do not want that.

    DefaultRowSorter sorter = ((DefaultRowSorter) jTable1.getRowSorter());
    ArrayList list_ar = new ArrayList();
    list_ar.add(new RowSorter.SortKey(2, SortOrder.ASCENDING));
    sorter.setSortKeys(list_ar);
    sorter.sort();

I tried experimenting and manipulating the arraylist ( list_ar ) but the Arraylist when converted into Object array, gives addresses as its elements. Please recommend me a proper way of performing such operations.

The code below is for sorting alphabetically. But I do not want that.

Don't store a String in the TableModel.

Instead store an Integer value. You will also need to override the getColumnClass() method of the TableModel to return Integer.class for that column. Then the JTable will use the appropriate Comparator to sort the integer values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM