• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Pagination using resultset to populate a tablemodel

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I solved the problem by changing the SELECT



Thanks for your help
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic