• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTable refreshing problem

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()
{

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
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
row.removeAllElements();
while (blah bleh){
row.addElement(new data);
}
table.setTableData(row);
Good luck
Polina
 
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic