开发者

Comparator for a specific column in JTable

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

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.


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.

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


EDIT: 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.


In this example, class Value implements Comparable<Value>.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜