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.
A few very good hints here!
Try using GlazedList .. it just invloves few line of code and very neat .Glazedlist
精彩评论