jQuery in Action, 3rd edition
The moose likes Swing / AWT / SWT and the fly likes JTable and JScrollPane 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 and JScrollPane problem" Watch "JTable and JScrollPane problem" New topic

JTable and JScrollPane problem

Andrew Cubides

Joined: May 09, 2001
Posts: 1
I have a JScrollPane containing a JTable.... I have an "Add New Row" button that creates a new blank row in the JTable... my problem is that the scrollpane doesn't seem to update when the table keeps growing due to added rows. I can keep adding rows but then they disappear below the scrollpane and I can't scroll down to see them.
Here is the code from the action listener of the "Add New Row" button:
Vector v = new Vector();
DefaultTableModel dfm = (DefaultTableModel)table.getModel();
int rowCount = table.getRowCount();
As you can see I tried every method I could think of to try and get the ScrollPane to update.
Any insight would be great, thank you...
Ranch Hand

Joined: Jul 06, 2001
Posts: 257
What on earth are you doing? First of all, you need to understand Swing's Model/Delegate framework. When the data model of the table is changed, the table view is automatically updated. You are not responsible to update the layouts, fire events, or clean the kitchen sink.
You should initialize your table, table model and scroll pane like this...
DefaultTableModel dtm = new DefaultTableModel();
JTable t = new JTable( dtm);
JScrollPane sp = new JScrollPane( t);
//then add the scroll pane to your panel and you're ready to go!
After this code, you can literally forget about the table itself provided that you keep reference to the table model because all of your updates should occur through the model and not through the table itself. You can add rows, delete them, remove all rows, anything you want, and the table view will be updated for you.
Hope it helps
San Su
Ranch Hand

Joined: Jul 06, 2001
Posts: 313
Andrew Cubides,
What you are doing is correct except the following unnecessary lines
You dont need those. You dont need to set the table model each and every time when you add a row in your table.
Ok Let's come to the problem, I am pretty sure that you should
be getting "ArrayIndexOutOfBoundsException"( i am using JDK1.2.2). I dont know whether it's a bug or not. Set the
size of your vector equals to the column count. That will
solve your problem. If not, let me know..

[This message has been edited by Sankar Subbiah (edited July 10, 2001).]
I agree. Here's the link: http://aspose.com/file-tools
subject: JTable and JScrollPane problem
It's not a secret anymore!