[英]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.
例如:如果给出一个字符串,我将其转换为其ASCII值的总和。 And then I want to sort the JTable as per the sums of the ASCII values.
然后我想按照ASCII值的总和对JTable进行排序。 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. 我尝试进行实验和操作arraylist(
list_ar
),但是将Arraylist转换为Object数组时, list_ar
地址作为其元素。 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. 不要在TableModel中存储字符串。
Instead store an Integer
value. 而是存储一个
Integer
值。 You will also need to override the getColumnClass()
method of the TableModel to return Integer.class
for that column. 您还需要重写TableModel的
getColumnClass()
方法以返回该列的Integer.class
。 Then the JTable will use the appropriate Comparator to sort the integer values. 然后,JTable将使用适当的Comparator对整数值进行排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.