对最后一行除了JTable排序 [英] Sort JTable Except for Last Row

查看:89
本文介绍了对最后一行除了JTable排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 JTable ,其中最后一行是聚合所有其他行的总行。当用户单击表上的列标题时,行将按该列排序,但总行应始终位于底部。

I have a JTable where the last row is the total row that aggregates all other rows. When the user clicks a column header on the table, rows are sorted by that column, except for the total row which should be always at bottom.

是否有一种简单的方法用 TableRowSorter 实现这个?

Is there a simple way to implement this with TableRowSorter?

推荐答案

以下解决方案对我有用....

The following solution worked for me....

在您的表模型中更新 getRowCount()成员返回少于所需的1行。

In your table model update the getRowCount() member to return 1 row less than required.

然后修改分拣机报告的索引和行数,如下所示......

Then modify the index and row counts reported by your sorter as follows...

TableRowSorter<TableModel> sorter =
    new DefaultTableRowSorter<TableModel>(this.getModel()) 
{
    public int convertRowIndexToModel(int index)
    {
        int maxRow = super.getViewRowCount();
        if (index >= maxRow)
            return index;
        return super.convertRowIndexToModel(index);
    }

    public int convertRowIndexToView(int index) 
    {
        int maxRow = super.getModelRowCount();
        if (index > maxRow)
            return index;
        return super.convertRowIndexToView(index);
    }

    public int getViewRowCount() 
    {
        return super.getViewRowCount() + 1;
    }
};

myTable.setRowSorter(sorter);

这篇关于对最后一行除了JTable排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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