JTable中特定列的比较器 [英] Comparator for a specific column in JTable

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

问题描述

如何为JTable中的特定列设置自定义比较器?

How can I set a custom comparator for a specific column in JTable?

表的第三列包含双精度值的String表示,我想为该列创建一个比较器,以便单击该列的标题时,它将根据该比较器进行排序.

The third column of my table contains String representaion of double values and I want to creat a comparator for that column so that when I click on the header of that column it will sort according to that comparator.

推荐答案

第一个问题是,如果要管理双打,为什么要处理字符串.如果您使用双打并在CellRenderer中为该列设置格式,那会更好.

The first question is why, if you are managing doubles, you are dealing with Strings. It should be better if you used the doubles and just set the format in the CellRenderer for that column.

无论如何,您正在寻找的可能是这样的: http://download.oracle.com/javase/tutorial/uiswing/examples/components/TableSorterDemoProject/src/components/TableSorter.java

Anyway, what you are looking for may be this: http://download.oracle.com/javase/tutorial/uiswing/examples/components/TableSorterDemoProject/src/components/TableSorter.java

如果从Double到您的表示形式的转换很复杂,我将创建一个Comparable类,其中包含Double数和String表示形式. Equals(),hashcode()和compareTo()将使用double的值实现; cellRenderer()和toString()将使用String表示形式.

If somehow the translation from Double to your representation is complicated, I would create a Comparable class that contains both the Double number and the String representation. Equals(), hashcode() and compareTo() would be implemented using the value of the double; the cellRenderer() and toString() would use the String representation.

这篇关于JTable中特定列的比较器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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