• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTABLE REFRESH PROBLEMS  RSS feed

 
R Ludington
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
}
 
Sebastiaan Kortleven
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the data changes, use the TableModelInstanceGoesHere.fireTableDataChanged() to update the data in the JTable...
 
R Ludington
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is my point ... I dont know how to do that...
Could you tell me how ???
 
Sebastiaan Kortleven
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm... You shouldn't build a new JTable each time you want to display something..
Create a method that builts the components (add the jtable to the panel etc)
Have a method called updateData(ResultSet rs), in this method, refactor the resultset to a double array of whatever you want in your table (as you did the first time).. do a getModel on the JTable, setDataVector with the data you just calculated and then do a fireTableDataChanged() on that model..
So you just change the data the JTable is showing...
Also, you don't need the revalidate and repaint method calls.. fireTableDataChanged will repaint the JTable with the new data
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!