使用 JTable 正确排序双精度数
我有一个表,其中第一列包含字符串,第二列包含双精度数。
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);
双精度数按字符串排序,因此 1 < > 15< 2< 25< 3. 我该如何解决这个问题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
关键在于 如何使用表格——概念:编辑器和渲染器。只需确保您的第二列实际上包含
双
值。可用的valueOf()
方法可用于使类型显式化。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 availablevalueOf()
methods may be used to make the type explicit.这里有一些非常好的提示!
A few very good hints here!
尝试使用 GlazedList ..它只需要几行代码并且非常整洁。Glazedlist
Try using GlazedList .. it just invloves few line of code and very neat .Glazedlist