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?
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?