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

My JTable is a bit confused

matthew weeks
Greenhorn

Joined: Feb 23, 2004
Posts: 15
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;
}
clio katz
Ranch Hand

Joined: Apr 30, 2004
Posts: 101
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: My JTable is a bit confused