简体   繁体   English

排序多头的JTable列

[英]Sort JTable column of longs

I'm trying to sort a column in my JTable. 我正在尝试对JTable中的列进行排序。 The column contains Long's (j ava.util.Long ), which implement Comparable. 该列包含Long(j ava.util.Long ),它实现Comparable。 Therefore, reading this document , it says: 因此,阅读此文档时 ,它说:

1: If a Comparator has been specified for the column by the setComparator method, use it. 1:如果通过setComparator方法为该列指定了Comparator,请使用它。

2: If the column class as returned by getColumnClass is String, use the Comparator returned by Collator.getInstance(). 2:如果getColumnClass返回的列类为String,请使用Collat​​or.getInstance()返回的Comparator。

3: If the column class implements Comparable, use a Comparator that invokes the compareTo method. 3:如果列类实现Comparable,则使用一个Comparator来调用compareTo方法。

4: If a TableStringConverter has been specified, use it to convert the values to Strings and then use the Comparator returned by Collator.getInstance(). 4:如果已指定TableStringConverter,则使用它将值转换为String,然后使用Collat​​or.getInstance()返回的Comparator。

5: Otherwise use the Comparator returned by Collator.getInstance() on the results from calling toString on the objects. 5:否则,对对象调用toString的结果使用Collat​​or.getInstance()返回的Comparator。

My code does not create a custom Comparator object, so #1 is out. 我的代码没有创建自定义Comparator对象,因此#1出局了。 The column is a column of Long's so #2 is out. 该列是Long的列,因此#2不在列。 #3 states that it should sort by the Long "compareTo" method. #3指出应按Long“ compareTo”方法排序。 But it doesn't. 但事实并非如此。 If my JTable has 3 Longs, 90,900, and 111, it will sort them, "900,90,111" or "111,90,900". 如果我的JTable有3个Long,90,900和111,它将对其进行排序,即“ 900,90,111”或“ 111,90,900”。 It appears to be sorting them like strings, as stated in #5. 如#5所述,它们似乎像字符串一样对它们进行排序。

Here is out we create our table: 这里是我们创建表的地方:

table = new JTable( new CustomTableModel( new Vector<Vector<Object>>() ,Record.getNames() ) );
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.setAutoCreateRowSorter(true);

And then adding info: 然后添加信息:

public void setRecords( Iterable<Record> records ){
    CustomTableModel model = (CustomTableModel) table.getModel();
    model.setRowCount(0);
    model.clearRecords();

    for( Record r : records ){
        Vector<Object> v = new Vector<Object>();
        v.add(r.getFromNumber());
        v.add(r.getToNumber());
        v.add(r.getStartDate());
        v.add(new Long( r.getDuration() ) );
        model.addRow(v);
        model.addRecord(r);
    }
    model.fireTableDataChanged();
    table.getRowSorter().toggleSortOrder(2);

How can I fix it, so the last column (column 3), is sorted by Long/long/int, and not by string? 我该如何解决它,所以最后一列(第3列)是按Long / long / int而不是按字符串排序的? I looked into custom comparators, but I wasn't sure how to implement. 我研究了自定义比较器,但不确定如何实现。 Shouldn't the JTable use #3? JTable不应该使用#3吗? Thanks! 谢谢!

验证模型的getColumnClass()方法是否返回Long.class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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