This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JTable - Adding Row Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable - Adding Row" Watch "JTable - Adding Row" New topic

JTable - Adding Row

Shivaji Bhosale
Ranch Hand

Joined: Mar 12, 2001
Posts: 70
I have 2 classes, Model class & Panel class having JTable instance.
In that Panel class, I did,
This model_obj is passed to the JTable instance.
From this same class I am populating row in model class.
With an action event for a button, I am inserting row into model. There I call rowInsert function from model class which having,
fireTableChanged(new TableModelEvent(this));
/// this = Object of model class.
which works fine, calling tableChanged method from my Panel class. This even gives me info, such as, event raised is for insertion.
In this tableChanged method, I called updateUI() for Panel.
All works with no exception anywhere. But inserted row is not getting displayed over Panel. Initially I am having no rows.
Please Note : table object is added to the ScrollPane, which in turn added to Panel. Component addition code is perfect.
Where can I be wrong ?
waiting impatiently !!!
Swamy Vatti
Ranch Hand

Joined: May 02, 2001
Posts: 57
Try to revalidate/repaint the table after calling the addRow method in the actionPerformed of the button.

Renee Zhang
Ranch Hand

Joined: Sep 10, 2001
Posts: 72
Please try to use fireTableRowsInserted(rowNumber, rowNumber) and fireTableRowsDeleted(rowNumber, rowNumber) in your tableModel instead of fireTableChanged.
Good luck!
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
you don't need to call any repaint or update methods. the model should notify the table on its own:
"Each JTable [(in the Creating a GUI with JFC/Swing trail)] object has a table model that holds its data. When a table model listener is registered on the table model, the listener is notified every time the table model's data changes. The JTable itself automatically uses a table model listener to make its GUI reflect the current state of the table model."
Shivaji Bhosale
Ranch Hand

Joined: Mar 12, 2001
Posts: 70
Thanx guys for your valuable comments.
Indeed problem found to be different. I have added table to Panel. ScrollPane to ScrollPane.
Since initially table having no rows Panel as well the ScrollPane were at their min position.
After I add row to model, I used to get of type Row Added to the Table, but I could never see that added row, until, I dragged my SplitPane below (resize).
It was indeed silly thing to have Panel within scrollpane.
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
If your using swing (JTable, JScrollPane, JPanel), then it is necessary to add the table to the scrollpane if you ever want to see any columnheaders.
I agree. Here's the link:
subject: JTable - Adding Row
It's not a secret anymore!