Forums Register Login

Pagination using resultset to populate a tablemodel

+Pie Number of slices to send: Send
I am new to Java and need some help in pagination using a Resultset to populate a Tablemodel

I have a class that creates an Object called Resultsetframe which is extended from Jframe

On this I put a combobox with a list of database tables from a database that I connect to using JDBC.

When I choose a table, The ActionPerformed method fires.
I get the number of pages from the table based on 1000 records to a page
I create a new ResultTableModel based on AbstractTableModel

[CODE ONE]
model = new ResultSetTableModel(this);


JTable table = new JTable(model);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

TableCellRenderer rend = table.getTableHeader().getDefaultRenderer();
TableColumnModel tcm = table.getColumnModel();
for (int j=0; j < tcm.getColumnCount(); j+=1) {
TableColumn tc = tcm.getColumn(j);
TableCellRenderer rendCol = tc.getHeaderRenderer(); // likely null
if (rendCol == null) rendCol = rend;
Component c = rendCol.getTableCellRendererComponent(table, tc.getHeaderValue(), false, false, 0, j);
tc.setPreferredWidth(c.getPreferredSize().width);
}

scrollPane = ResultSetTableModel.createPagingScrollPaneForTable(table);
getContentPane().add(scrollPane, BorderLayout.CENTER);
pack();
doLayout();
[/CODE]


The constructer calls newResultset() which gets the first/next 1000 records
These records are displayed in the scroll pane

[CODE TWO]
public ResultSetTableModel(ResultSetFrame aResultSetFrame)
{ rsf = aResultSetFrame;
rs = newResultSet();
fireTableDataChanged();
System.out.println("constructor");

try
{ rsmd = rs.getMetaData();
}
catch(SQLException e)
{ System.out.println("Error " + e);
}
}
{/CODE]

Here is newResultSet

[CODE THREE]
protected ResultSet newResultSet()
{
try
{
System.out.println("get resultset " + pageOffset);

if (rs != null) rs.close();
String query = "SELECT * FROM " + rsf.tableName + " where rownum >= " + (pageOffset * 1000) +
" and rownum < " + ((pageOffset * 1000) + 1000);
rs = rsf.stmt.executeQuery(query);

System.out.println(query);

return rs;
}
catch(SQLException e)
{System.out.println("Error " + e);
return null;
}
}
[/CODE]

Then control comes back to the class where I create the Resultsetframe.
I create the Jtable, I get the table cells, I add a scroolpane
and call pack() and dolayout()

In the ResultSetTableModel class I have a button called PageDown to get the next result set to populate the TableModel
{CODE FOUR]
public void pageDown()
{
System.out.println("pagedown " + pageOffset);

if (pageOffset < rsf.pageCount - 1)
{
pageOffset++;
rs = newResultSet();
fireTableDataChanged();
}
}
{/CODE]
This calls newResultSet like the constructor. When I page down, The scroll pane is blank, even though the pageOffset in incrementing
When I page up (I also have a pageup button) to the first page, it displayed the 1000 records from the resultset gotten by the
constructor in CODE ONE.

Anybody know why each subsequent page down results in a blank scroll pane?
Doesn't fireTableDataChanged() repopulate the Tablemodel?
+Pie Number of slices to send: Send
are you sure you count your pages correctly? (do you have more that 1000 records?) Add some println statements or do a debug, and see if the newResultSet method is called when you page down.
[ April 20, 2007: Message edited by: Andrei Pat ]
+Pie Number of slices to send: Send
I solved the problem by changing the SELECT



Thanks for your help
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1690 times.
Similar Threads
JTable
JRadioButton in JTable
iscelleditable is not working in jtable
JTable
Slight JTable problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 09:33:01.