aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Adding rows to Jtable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding rows to Jtable" Watch "Adding rows to Jtable" New topic
Author

Adding rows to Jtable

Anand Shrivastava
Ranch Hand

Joined: Jul 22, 2007
Posts: 125
Dear friends,

I have a JTable with DefaultTableModel like this


it does not reflect in the table.

Kindly help.


Anand Shrivastava
SCJA
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
If you call tbm.fireTableRowsInserted(0, 0) then the table will be notified that the model has changed. The model can't fire the event itself because you have modified its Vectors behind its back.

Simpler would be to use the model's methods instead: tbm.addRow() or tbm.insertRow(). [also: tbm.addColumn()] Not only will these fire events for you automatically but I think they do some error-checking, which could be handy if you are adding a row of length 1 to a 2-column table model.

[edit: changed tbm.fireTableRowsInserted(1, 1) to tbm.fireTableRowsInserted(0, 0), see below]
[ October 09, 2007: Message edited by: Brian Cole ]

bitguru blog
Anand Shrivastava
Ranch Hand

Joined: Jul 22, 2007
Posts: 125
I tried to call these methods by saying

jtb.getModel().addRow();

but it says cannot find symbol addRow();
Anand Shrivastava
Ranch Hand

Joined: Jul 22, 2007
Posts: 125
sorry the variable is not jtb in this case. I mean

jTable1.getModel().addRow();

It says cannot find symbol addRow()
Anand Shrivastava
Ranch Hand

Joined: Jul 22, 2007
Posts: 125
and even after saying tbm.fireTableRowsInserted(1,1); the change is not being reflected.
Anand Shrivastava
Ranch Hand

Joined: Jul 22, 2007
Posts: 125
and even if i do like tbm.addRow(row);
still the change is not reflected in the table.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Anand Shrivastava:
and even after saying tbm.fireTableRowsInserted(1,1); the change is not being reflected.


Oops, it should actually be tbm.fireTableRowsInserted(0, 0).

That will work, but you'll still get an ArrayIndexOutOfBoundsException because you're adding a row of length 1 to a two-column table model.

and even if i do like tbm.addRow(row);
still the change is not reflected in the table.


You named the Vector for the row you're adding r, so did you try tbm.addRow(r)? If so it would have worked (and with no ArrayIndexOutOfBoundsException).


Either of those two things will make it work, but easiest to rewrite it using the DefaultTableModel methods I mentioned:

Anand Shrivastava
Ranch Hand

Joined: Jul 22, 2007
Posts: 125
Thanks a lot,

It worked.
Archana Honnavalli
Ranch Hand

Joined: Feb 26, 2008
Posts: 39
...
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Archana,
I am sorry but I had to edit your post. What you had done is called hijacking a post.
http://faq.javaranch.com/java/UseOneThreadPerQuestion

Please start a new thread for your question.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Archana Honnavalli
Ranch Hand

Joined: Feb 26, 2008
Posts: 39
Maneesh Godbole wrote:Archana,
I am sorry but I had to edit your post. What you had done is called hijacking a post.
http://faq.javaranch.com/java/UseOneThreadPerQuestion

Please start a new thread for your question.


Ok i realize that.......
I have started a new thread for my question.
But the reason i posted there was that my doubt was related to the posts.
Anyway thanks for info

Archana
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Archana Honnavalli wrote:
But the reason i posted there was that my doubt was related to the posts.


In such cases, you can always provide a link to the relevant topic in your post.
Vineet kumar Gupta
Greenhorn

Joined: Dec 18, 2011
Posts: 16
But how to display this table on a button click....
I mean I am not getting the table on button click...
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

What didn't you understand in Maneesh's post about using one thread per question? Your question isn't even related to the question asked in this thread. Please start a new thread.

Also, please check your private messages for an important administrative matter.


luck, db
There are no new questions, but there may be new answers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding rows to Jtable