• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTable refreshing problem

matthew weeks
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I seem to have a problem refreshing the data within my JTable. I know the code is being run through.
I have a JTable which is populated from a selection in a combo box (this is a result set of a query using JDBC), on first click within the combo box the table displays the correct data. In no data is found for the JTable to display a dialog box is prompted to the user (from this i know that the code is generally working and being run through) which states that no records were found. Subsequent clicks either bring up the dialog box saying that "no records were found" (which are correct) or selecting a value within the combo box that should display records only displays the data from before and never changes.
So basically the data within the JTable doesnt refresh. From searching the forum, ive found similar problems which mention something like "fireTableDataChanged" which i cannot find within java.
The code that Im using to populate the JTable is below:-
public void tableDisplayQueryResults()


String query = "SELECT Courses.CourseCode, LessonName.LessonName, Staff.StaffSName, LessonTimes.LessonTimes, LessonTimesEnd.LessonTime, Lessons.LessonDateStart, Lessons.LessonDateEnd " +
"FROM (LessonTimesEnd RIGHT JOIN (LessonTimes RIGHT JOIN ((Courses RIGHT JOIN (Rooms RIGHT JOIN Lessons ON Rooms.RoomName = Lessons.RoomNumber) ON Courses.CourseCode = Lessons.CourseID) LEFT JOIN Staff ON Lessons.StaffID = Staff.StaffID) ON LessonTimes.LessonTimeID = Lessons.LessonTimeStart) ON LessonTimesEnd.LessonTimeID = Lessons.LessonTimeEnd) LEFT JOIN LessonName ON Lessons.LessonNameID = LessonName.LessonNameID " +
"WHERE (((Rooms.RoomName)='" + convertedRoomNumberVector + "'))" +
"ORDER BY LessonTimes.LessonTimes, LessonTimesEnd.LessonTime, Lessons.LessonDateStart, Lessons.LessonDateEnd";
System.out.println("508 String query");

statementTable = connectionTable.createStatement();
resultSet = statementTable.executeQuery( query );
displayResultSet( resultSet );

catch ( SQLException sqlex ) {
txtaOutput.append("\n" + sqlex.toString());

//Table to display the bookings for the selected room
public void displayResultSet( ResultSet rs )
throws SQLException
// position to first record
boolean moreRecords = rs.next();

System.out.println("527 rs.next");
// If there are no records, display a message
if ( ! moreRecords ) {
JOptionPane.showMessageDialog( this,
"No bookings for this room" );
System.out.println("Dialog box");
Vector columnHeads = new Vector();
Vector rows = new Vector();
try {
// get column heads
ResultSetMetaData rsmd = rs.getMetaData();

for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
columnHeads.addElement( rsmd.getColumnName( i ) );

System.out.println("548 Obtain column heads");

// get row data

System.out.println("551 do get Rows");
do {
rows.addElement( getNextRow( rs, rsmd ) );
System.out.println("552 Get next row");
} while ( rs.next() );
// display table with ResultSet contents
table = new JTable( rows, columnHeads );
System.out.println("558 new JTable");
scroller = new JScrollPane(table);
scroller.setBounds(3, 3, 590, 150);

System.out.println("568 scroller ");


scroller, BorderLayout.CENTER );

System.out.println("576 validate");
//setVisible true
// getContentPane().setVisible(true);
catch ( SQLException sqlex ) {
txtaOutput.append("\n " + sqlex.toString());
private Vector getNextRow( ResultSet rs,
ResultSetMetaData rsmd )
throws SQLException

Vector currentRow = new Vector();
System.out.println("593 Add rows");
for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
switch( rsmd.getColumnType( i ) ) {
case Types.VARCHAR:
currentRow.addElement( rs.getString( i ) );
case Types.INTEGER:
new Long( rs.getLong( i ) ) );

txtaOutput.append("\nType was: " +
rsmd.getColumnTypeName( i ) );
System.out.println("605 ");

return currentRow;


please help me
Polina Bee
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Matthew.
I have just a guess. Looks like you create a new table every time. Did you try to remove the previous one from the content pane before adding a new one? You can also try to use the same table all the time and change just table date. Something like
while (blah bleh){
row.addElement(new data);
Good luck
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!