Sort JTable Except for Last Row

后端 未结 5 1252
盖世英雄少女心
盖世英雄少女心 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:38

    @Das: maybe this version is better since you do not need to override the getRowCount(), which may be used and maybe causing problems by other functions

    public class TableRowSorterTotal extends TableRowSorter
    {   
    
        TableRowSorterTotal(MyTableModel model)
        {
            super(model);
        }
    
        public int convertRowIndexToModel(int index)
        {
            int maxRow = super.getViewRowCount();
            int currModel = super.convertRowIndexToModel(index);
            int maxModel = super.convertRowIndexToModel(maxRow-1);
    
            if(currModel == maxModel)
                return maxRow - 1;
    
            if(currModel > maxModel)
                return currModel- 1;
    
            return currModel;
        }
    
        public int convertRowIndexToView(int index) 
        {
            int maxRow = super.getModelRowCount();
            int currView= super.convertRowIndexToView(index);
            int maxView = super.convertRowIndexToView(maxRow-1);
    
            if(currView == maxView)
                return maxRow - 1;
    
            if(currView > maxView)
                return currView- 1;
    
            return currView;
        }
    }
    

提交回复
热议问题