开发者

Correctly sorting doubles with JTable

开发者 https://www.devze.com 2023-01-07 13:39 出处:网络
I have a table where the first column contains Strings and the second contains doubles. stars = new StarDatabase(path);

I have a table where the first column contains Strings and the second contains doubles.

    stars = new StarDatabase(path);
    Object[][] data = new Object[stars.size()][2];
    int i = 0;
    for (String name : stars.keySet()) {
        data[i][0] = name;
        data[i++][1] = stars.get(name).period;
    }
    StarsTable = new JTable(data, StarsColumnNames);
    StarsTable.setAutoCreateRowSorter(true);

The doubles are sorted as strings, 开发者_开发技巧so 1 < 15 < 2 < 25 < 3. How can I fix this?


The key to this is found in How to Use Tables—Concepts: Editors and Renderers. Just make sure your second column actually contains Double values. Either of the available valueOf() methods may be used to make the type explicit.


A few very good hints here!


Try using GlazedList .. it just invloves few line of code and very neat .Glazedlist


0

精彩评论

暂无评论...
验证码 换一张
取 消