在JTable中排序double值 [英] Sorting double values in JTable

查看:155
本文介绍了在JTable中排序double值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经发现了很多与此相关的问题,但是我没有找到一个简单的解决方案来解决我的问题。



我找不到一种方法使我的JTable排序Double值正确。



我扩展了AbstractTableModel以接收一个Class数组,并返回每列正确的类型:

 code> class TableModelMod extends AbstractTableModel {

private ArrayList data;
private String [] headers;
private Class [] types;

TableModelMod(String [] heads,ArrayList datas,Class [] classes){
headers = heads;
data = datas;
types = classes;
}
...
@Override public Class getColumnClass(int c){
if(c> types.length - 1)
return null;
else
返回类型[c];
}
...

然后在我的自定义JTable构造函数中: / p>

  TableRowSorter< TableModelMod> sorter = new TableRowSorter< TableModelMod>((TableModelMod)getModel()); 

但是,当添加行时,我会收到此错误:

  java.lang.IllegalArgumentException:不能将给定的Object格式化为数字

在方法 DecimalFormat.format(Object number,StringBuffer toAppendTo,FieldPosition pos)中接受大多数数字类型但Double。



如果我为Double列使用另一个类,我没有收到错误,但仍然排序不按预期工作。我尝试使用不同的数字类,但没有看起来正确地排序双倍:

  @Override public Class getColumnClass(int c){
if(c> types.length - 1)
返回null;
else if(types [c] == Double.class)
return Number.class;
else
返回类型[c];
}

我不知道我是否需要实现一个自定义RowSorter ,一个自定义的CellRenderer或两者。



有人可以指导我如何解决这个更简单的方法?





EDITED:SOLVED



告诉问题是什么非常尴尬。 b

包含Object []行的ArrayList使用getString(int)而不是getObject(int)或getDouble(int)从数据库ResultSet中填充,因此该值不能被Double渲染器。很奇怪的是,它没有使用Integer或Number作为列类给出异常,但是它被排序为String。我正在寻找一个错误的课程,因为我确信我的ArrayList只包含对象。



非常感谢你的例子,看着他们,我注意到我的双打实际上是Strings,然后我可以找到转换发生的地方。

解决方案

查看此代码。它排序双重值。





  import java.awt.BorderLayout; 

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class RowSorterDemo {
public static void main(String args []){
JFrame frame = new JFrame(Sort Table Demo);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
对象行[] [] = {{J,23.1},{R,21.1,},{E,21.2,},{B,27.1,},{A ,25.2,},
{S,22.9,},};

字符串列[] = {名称,年龄};

TableModel model = new DefaultTableModel(rows,columns){
public Class getColumnClass(int column){
Class returnValue;
if((column> = 0)&&(column< getColumnCount())){
returnValue = getValueAt(0,column).getClass();
} else {
returnValue = Object.class;
}
return returnValue;
}
};

JTable table = new JTable(model);

RowSorter< TableModel> sorter = new TableRowSorter< TableModel>(model);

table.setRowSorter(sorter);

JScrollPane pane = new JScrollPane(table);

frame.add(pane,BorderLayout.CENTER);

frame.setSize(300,150);
frame.setVisible(true);
}
}

我修改了这个链接,以便它需要双重值。


I have found quite a few questions related to this but I haven't found a simple solution to my issue though.

I can't find a way to make my JTable sort Double values properly.

I extended AbstractTableModel to receive a Class array and return the proper types per column:

class TableModelMod extends AbstractTableModel{

    private ArrayList data;
    private String [] headers;
    private Class [] types;

    TableModelMod(String [] heads, ArrayList datas, Class [] classes){
        headers = heads;
        data = datas;
        types = classes;
    }
    ... 
    @Override public Class getColumnClass(int c){
        if (c > types.length - 1)
            return null;
        else 
            return types[c];
    }
...

And then in my custom JTable constructor:

TableRowSorter<TableModelMod> sorter = new TableRowSorter<TableModelMod>((TableModelMod)getModel());

But then I get this error when adding rows:

java.lang.IllegalArgumentException: Cannot format given Object as a Number

It fails at method DecimalFormat.format(Object number, StringBuffer toAppendTo, FieldPosition pos) that accepts most numeric types but Double.

If I use another class for the Double columns I get no error but still the sorting does not work as expected. I tried with different numeric classes but none seem to sort doubles correctly:

@Override public Class getColumnClass(int c){
    if (c > types.length - 1)
        return null;
    else if (types[c] == Double.class)
        return Number.class;
    else 
        return types[c];
}

I'm not sure if I what I need is to implement a custom RowSorter, a custom CellRenderer, or both.

Could someone guide me on how to fix this the simpler way?

Thanks a lot and best regards.

EDITED: SOLVED

It's quite embarrassing to tell where the problem was.

The ArrayList containing the Object[] rows was filled from a database ResultSet using getString(int) instead of getObject(int) or getDouble(int), therefore the value could not be used as Double by the renderer. It's weird that it didn't give exceptions using Integer or Number as column class, but it was being sorted as a String anyways. I was looking for an issue in the wrong classes since I was convinced my ArrayList contained just Objects.

Thanks a lot for your examples, looking at them I noticed my doubles were actually Strings and then I could find where the conversion was happening.

解决方案

Check out this code. It sorts double values.

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class RowSorterDemo {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Sort Table Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Object rows[][] = { { "J", 23.1 }, { "R", 21.1, }, { "E", 21.2, }, { "B", 27.1, }, { "A", 25.2, },
        { "S", 22.9, }, };

    String columns[] = { "Name", "Age" };

    TableModel model = new DefaultTableModel(rows, columns) {
      public Class getColumnClass(int column) {
        Class returnValue;
        if ((column >= 0) && (column < getColumnCount())) {
          returnValue = getValueAt(0, column).getClass();
        } else {
          returnValue = Object.class;
        }
        return returnValue;
      }
    };

    JTable table = new JTable(model);

    RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);

    table.setRowSorter(sorter);

    JScrollPane pane = new JScrollPane(table);

    frame.add(pane, BorderLayout.CENTER);

    frame.setSize(300, 150);
    frame.setVisible(true);
  }
}

I modified the source given at this link a little bit so that it takes double values.

这篇关于在JTable中排序double值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆