This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable - Adding Row" Watch "JTable - Adding Row" New topic
Author

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,
model_obj.addTableModelListener(this);
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.


Swamy
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
hi,
you don't need to call any repaint or update methods. the model should notify the table on its own:
http://java.sun.com/docs/books/tutorial/uiswing/events/tablemodellistener.html
"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."
chantal
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.
Thanx.
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.
chantal
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable - Adding Row
 
Similar Threads
JTable(!!!)
Jtable is not refreshing properly
Displaying an additional row in a jtable
JTable
Update database by editing the cells in JTable