tags:

views:

188

answers:

2

Hi,

There is a jbutton in my Jpanel , when i clicked it , it loads up my Jtable , sometimes a query return so many records (500 rows). So i wanna restrict it with 5 record.When query return i wanna count it if its higher than 5 then Jtable shows up only first 5 record when user click Forward button it will shows up next 5 record.When user click Back button it will show previous 5 record.

Can anyone help me ? Is there any example for this with TableModel ?

i need this urgently . thanx guyz.

+2  A: 

If you wish to load a large table, you may want to use a SwingWorker (details here) thread to load the table in the background. Loading a table with 500 rows should not be a problem. You can then put the data into a suitable object format and pass it to your TableModel.

If you decide to use a List for example, in your table model you could have two lists:

List allData
List viewData
int startIndex

The viewData list is what is referenced by the getValueAt(..) method in your implementation of the TableModel interface. The viewData list is always a subset (bound by startIndex, of length 5) of allData. When the user clicks "Next", your action listener could call a method on the Table model that increments startIndex by 5 (or whatever). You then regenerate your viewData instance so that it is the appropriate 5 row subset of allData, and call fireTableChanged(). This will be easy if you have extended AbstractTableModel in the first place.

This should be pretty straightforward to implement. I think its better than making a database call every time you want to get the next set of data. IMHO, its better to take a little bit more time upfront to preload the data.

Luhar
Adamski's approach above is more elegant than using 2 lists. I would still recommend using a SwingWorker to pre-load the data though.
Luhar
+2  A: 

I suggest implementing a "Paged" TableModel which provides a window onto the entire dataset and methods for moving forwards and backwards throughout the data. This way you do not require two Lists to store the data but rather a single List holding all data along with a marker to your current position; e.g.

public class ImmutablePagedTableModel extends AbstractTableModel {
  private final List<MyBusinessObject> allData;
  private final int pageSize;
  private int pos;  

  public ImmutablePagedTableModel(List<MyBusinessObject> allData) {
    // Copy construct internal list.  Use ArrayList for random access look-up efficiency.
    this.allData = new ArrayList<MyBusinessObject>(allData);
  }

  /**
   * Returns true if the model has another page of data or false otherwise.
   */
  public boolean hasNextPage() {
    return pos + pageSize < allData.size();
  }

  /**
   * Flips to the next page of data available.
   */
  public void nextPage() {
    if (hasNextPage()) {
      pos += pageSize;

      // All data in the table has effectively "changed", so fire an event
      // causing the JTable to repaint.  
      fireTableDataChanged();
    } else {
      throw new IndexOutOfBoundsException();
    }    
  }

  public int getRowcount() {
    return Math.min(pageSize, allData.size() - pos);
  }

  // TODO: Implement hasPreviousPage(), previousPage();
}

As 00rush mentions a more ambitious approach would be to use a SwingWorker to stream in the data in the background. You could still use the paged TableModel approach for this; you'd just need to ensure that appropriate TableModelEvents are fired as you append to the end of the allData list.

Adamski