posted 19 years ago
I have a piece of code where a user querys a SQL database and gets a resultset back. this resultset is passed to the following piece of code which puts it into a jtable and displays it. The first run through works fine but the each run after shows the original data. Sooo my Jtable is not being refreshed/repainted/. Please someone help me figure out what I am doing wrong. I am very new at this so If you could give me a bit more detail than something like (just use fire....) I have searched the net and tried everything that I have found but the easiest solution seems to be to use the setvector method for defaulttablemodel ... I just cant get it to work. Please I could use an experts help on this one.
Here is the code :
private void Display (ResultSet Result)throws SQLException
{
Vector ColumnNames = new Vector();
Vector rows = new Vector();
boolean Records = Result.next();
if (!Records )
{
JOptionPane.showMessageDialog( this, "No Data " );
setTitle( "PROCESS COMPLETED");
return;
}
setTitle ("QUERY RESULTS");
try
{
ResultSetMetaData MetaData = Result.getMetaData();
for ( int x = 1; x <= MetaData.getColumnCount(); ++x)
ColumnNames.addElement (MetaData.getColumnName ( x ) );
do
{
rows.addElement (DownRow( Result, MetaData ) );
} while ( Result.next() );
DataTable = new JTable ( rows, ColumnNames ) ;
JScrollPane scroller = new JScrollPane (DataTable) ;
getContentPane(). add ( scroller, BorderLayout.CENTER );
DefaultTableModel model = (DefaultTableModel)DataTable.getModel();
((DefaultTableModel)model).setDataVector( rows, ColumnNames ) ;
model.newDataAvailable(null);
DataTable.revalidate();
DataTable.repaint();
}
catch (SQLException error )
{
System.err.println("Data display error." + error);
System.exit(4);
}
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setSize(screenSize.width, screenSize.height);
setLocation(1,1);
DataTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
show();
}
private Vector DownRow ( ResultSet Result, ResultSetMetaData MetaData)
throws SQLException
{
Vector currentRow = new Vector();
for ( int x = 1; x <= MetaData.getColumnCount(); ++x )
{
switch ( MetaData.getColumnType ( x ) )
{
case Types.VARCHAR :
currentRow.addElement( Result.getString ( x ) ) ;
break;
}
}
return currentRow;
}