Multipage JTable: impossible to display less items than rows

前端 未结 3 1916
鱼传尺愫
鱼传尺愫 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.

    0 讨论(0)
  • 2021-01-27 12:52

    Make sure your model's getRowCount method is inline with what you want it to do. The getRowCount method should return an acceptable number for your table, so that it doesn't call getValueAt for any rows that don't exist. So, if you have no row 14, your row count shouldn't be that high.

    0 讨论(0)
  • 2021-01-27 12:54

    I think that not easy job, I suggessting to look at aephyr's code, maybe more easies way is implementing this code, but for real effect you have to lock JScrollBars, swith to NEVER

    0 讨论(0)
提交回复
热议问题