aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTable refreshing problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable refreshing problem" Watch "JTable refreshing problem" New topic
Author

JTable refreshing problem

matthew weeks
Greenhorn

Joined: Feb 23, 2004
Posts: 15
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()
{

try
{

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 );
statementTable.close();

}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
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");
return;
}
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.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroller.setAutoscrolls(true);
scroller.setBounds(3, 3, 590, 150);
scroller.setLocation(30,250);



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

getContentPane().add(scroller);

//jScrollPane2.add(table);
getContentPane().add(
scroller, BorderLayout.CENTER );

validate();
System.out.println("576 validate");
//setVisible true
// getContentPane().setVisible(true);
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
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 ) );
System.out.println("599");
break;
case Types.INTEGER:
System.out.println("602");
currentRow.addElement(
new Long( rs.getLong( i ) ) );

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

return currentRow;

}

please help me
Polina Bee
Greenhorn

Joined: Feb 17, 2004
Posts: 6
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
row.removeAllElements();
while (blah bleh){
row.addElement(new data);
}
table.setTableData(row);
Good luck
Polina
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JTable refreshing problem