Sort JTable Except for Last Row

后端 未结 5 1254
盖世英雄少女心
盖世英雄少女心 2020-12-03 23:54

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 c

5条回答
  •  醉酒成梦
    2020-12-04 00:33

    The following solution worked for me....

    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 sorter =
        new DefaultTableRowSorter(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);
    

提交回复
热议问题