File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Pagination using resultset to populate a tablemodel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Pagination using resultset to populate a tablemodel" Watch "Pagination using resultset to populate a tablemodel" New topic
Author

Pagination using resultset to populate a tablemodel

Mike Gallant
Greenhorn

Joined: Apr 19, 2007
Posts: 2
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?
Andrei Pat
Greenhorn

Joined: Apr 19, 2007
Posts: 13
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 ]
Mike Gallant
Greenhorn

Joined: Apr 19, 2007
Posts: 2
I solved the problem by changing the SELECT



Thanks for your help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pagination using resultset to populate a tablemodel