[英]Sorting numbers in a JTable
如何實現對 JTable 中僅包含數字的列進行排序? 有 class TableRowSorter
。 但是,使用它會導致以下結果:對於每個數字,通過調用 toString 來獲取字符串表示,然后進行比較。 我收到的是例如:
100, 13, 2, 22, 9 而不是 2, 9, 13, 22, 100
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table);
table.setRowSorter(sorter);
為了避免這種情況,有以下方法:
sorter.setComparator(column,comparator);
由於我的數字只有 Integer、Long 和 Double,我不明白為什么要定義標准 java.lang 類的不起眼的 Comparator 。 有沒有更簡單的方法?
您應該為列的 class 定義 Number。
見表模型
public Class<?> getColumnClass(int columnIndex);
您的表 model 是如何定義的? If the getColumnClass
method return Integer.class
(or Long.class
, or Number.class
, depending on what the column contains), then the sort should be right, and you shouldn't have to configure any specific row sorter.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.