[英]JTable and sorting
I have little problem with sorting my JTable, I can't get the functionality I want. 排序我的JTable我没有什么问题,我无法获得我想要的功能。 In my window I have something like total commander, in one column there are files and if there is any, parent folder in the other one there is or number of bytes.
在我的窗口中,我有类似总指挥官的东西,在一列中有文件,如果有任何,父文件夹在另一个中有或字节数。 What I want is, if I click on File Name column I want the parent folder always on top, then ascending/descending folder names and then ascending/descending file names and if I click on File Type I want directories to remain untouched and then I want normal files in ascending/descending order by their size.
我想要的是,如果我点击文件名列,我希望父文件夹始终在顶部,然后升序/降序文件夹名称,然后升序/降序文件名,如果我点击文件类型我希望目录保持不变,然后我希望普通文件按其大小按升序/降序排列。
I tried to toy with RowSorters, my own comparators but as I said I can't get what I want, should I catch the event myself, then manually sort values and update my model (which I use btw.)? 我试着玩RowSorters,我自己的比较器,但正如我所说,我无法得到我想要的东西,我应该自己抓住事件,然后手动排序值并更新我的模型(我使用顺便说一句。)? Is there some elegant way to do what I want?
有什么优雅的方式来做我想要的吗?
It sounds like you might want to use a tree table instead. 听起来你可能想要使用树表。 Please take a look at: http://java.sun.com/products/jfc/tsc/articles/treetable1/
请查看: http : //java.sun.com/products/jfc/tsc/articles/treetable1/
If you want to use JTable itself then you can try to implement sorting in table model as it might be easier. 如果您想使用JTable本身,那么您可以尝试在表模型中实现排序,因为它可能更容易。
TableRowSorter<TableModel> sorter =
new TableRowSorter<TableModel>(getModel()) {
Map<Integer, SortKey> keys = new HashMap<Integer, SortKey>();
public void toggleSortOrder(int column)
{
SortKey key = keys.get(column);
SortOrder order = null;
// Get last sort order.
if (key != null) {
if (key.getSortOrder() == SortOrder.DESCENDING){
order = SortOrder.ASCENDING;
}
else {
order = SortOrder.DESCENDING;
}
}
else {
order = SortOrder.DESCENDING;
}
keys.put(new SortKey(column, order));
getTableModel().sort(keys);
}
};
setRowSorter(sorter);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.