Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

My JTable is a bit confused

matthew weeks
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JTable which displays the results of queries carried out upon a database, with the results being displayed in a JTable. This works fine, until the JTable has to display records which require the vertical scroll bar to show them all.
When this happens the first lot of records are displayed, trying to scroll down cannot be done. If the window is minimised then made active again, it goes back to the previous set of results being displayed.
Help me please , the code that Ive used for the table is below -
public void displayResultSet( ResultSet rs )
throws SQLException
// position to first record
boolean moreRecords =;

System.out.println("Do records exist 567");
// If there are no records, display a message
if ( ! moreRecords )

JOptionPane.showMessageDialog( this,
"No bookings for this Room or Staff member" );
System.out.println("Dialog box");

Vector columnHeads = new Vector();
Vector rows = new Vector();
try {
// get column heads
ResultSetMetaData rsmd = rs.getMetaData();
System.out.println("548 Obtain column heads");

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

// get row data

System.out.println("551 do get Rows");
do {
rows.addElement( getNextRow( rs, rsmd ) );

System.out.println("552 Get next row");
} while ( );
// 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");

catch ( SQLException sqlex ) {
txtaOutput.append("\n " + sqlex.toString());
private Vector getNextRow( ResultSet rs,
ResultSetMetaData rsmd )
throws SQLException

Vector currentRow = new Vector();
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;
clio katz
Ranch Hand
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi matthew,

this may be too little and too late, but ... since i didn't see other replies...

my first question might be whether you meant to add the scroller to the contentpane twice?

second question might be why the scroller needs so much configuration.

in most cases i can get scrolling working ok by setting preferred scrollable viewport size and adding the scrollpane. is there some reason for the other configurations you're using?

(once the scrollbar init is clear, it seems that there may be other issues related to display (e.g. when repaint can be safely invoked) and jtable overrides for setting/getting that might be need to be looked at)

(almost forgot: is your code firing table changed events elsewhere?)
[ May 13, 2004: Message edited by: clio katz ]
I agree. Here's the link:
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic