• 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

My JTable is a bit confused

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

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");
return;
}

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 ( 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,325);

System.out.println("568 scroller ");
table.repaint();
getContentPane().add(scroller);

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

table.repaint();
validate();
System.out.println("576 validate");

}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
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 ) );
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;
}
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)

hth
(almost forgot: is your code firing table changed events elsewhere?)
[ May 13, 2004: Message edited by: clio katz ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic