简体   繁体   中英

Correctly sorting doubles with JTable

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.

这里有一些很好的提示!

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


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