R
R
roman38472014-04-25 11:21:07
Java
roman3847, 2014-04-25 11:21:07

JTable Model - is it possible to display the nth number of records?

Is it possible to display, for example, the first 5 records, and then, by pressing the record button, and hide the first ones?

Those. it turns out something like pages, on the first 5 records, and on the second - the rest. Can it be done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dm13y, 2014-04-25
@roman3847

To do this, you can use the getRowCount () and getValueAt () methods.
You need to create variables with the number of pages and the current page, then, based on them, calculate the necessary rows from the array and display them.
For example:

public class TestTable extends AbstractTableModel {
    
    //Количество строк на странице
    private int rowOnPages = 10;
    //Текущая страница
    private int currentPages;
    //Количество страниц
    private int countPages;
    
    private ArrayList<String> data;
    private ArrayList<String> columnHeader;
    
    public TestTable(ArrayList<String> data, ArrayList<String> columntHeader){
        countPages = data.size() / rowOnPages;    
        this.data = data;
        this.columnHeader = columntHeader;
    }
    

    @Override
    public int getRowCount() {
        return rowOnPages;
    }

    @Override
    public int getColumnCount() {
        return columnHeader.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0:
                return data.get(rowOnPages * currentPages + rowIndex);
            default:
                return null;
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question