Multipage JTable: impossible to display less items than rows

前端 未结 3 1924
鱼传尺愫
鱼传尺愫 2021-01-27 12:18

I realized a JTable with a custom AbstractTableModel for implementing paging. I wanna show 5 item per page, but I\'ve a problem: if I have N item to show (with N wh

3条回答
  •  有刺的猬
    2021-01-27 12:42

    Just pin the the value to its acceptable maximum:

    realRow = Math.min(realRow, getRowCount());
    

    Addendum: In the example cited, implement getValueAt() as follows:

    // Work only on the visible part of the table.
    public Object getValueAt(int row, int col) {
        int realRow = row + (pageOffset * pageSize);
        if (realRow < data.length) {
            return data[realRow].getValueAt(col);
        } else {
            return null;
        }
    }
    

    Also consider BasicArrowButton.

提交回复
热议问题