Comparator for a specific column in JTable

纵然是瞬间 提交于 2019-12-12 12:28:12

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/6781889/comparator-for-a-specific-column-in-jtable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!