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

How to add rows to jTable dynamically?

Archana Honnavalli
Ranch Hand

Joined: Feb 26, 2008
Posts: 39
Hi,
Stuck in this jTable and swing issue. Please help.

My code is as below:

private javax.swing.JTable jTable1;
javax.swing.table.DefaultTableModel model = new javax.swing.table.DefaultTableModel();
jTable1 = new javax.swing.JTable(model);
jTable1.setModel(new javax.swing.table.DefaultTableModel(new Object[model.getRowCount()][], new String[] { "col1", "col2", "col3" }));
model.insertRow(0, new Object [] {"A", "A", "A"});
model.insertRow(0, new Object [] {"B", "B", "B"});

But when i execute this code, the 2 rows added are not getting reflected in jTable. Because in my screen i can only see a table with 3 columns and no rows.
I guess the model object has these rows but its not reflected in jTable1 object.

Also i have to mention rowCount in my setModel method else it gives compilation error ("Variables must be initialized error")
Please tell me how do i mention this count dynamically and reflect on my screen.

Thanks
Archana
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2172
    
    7
I guess the model object has these rows but its not reflected in jTable1 object.


Because you have two TableModels. You add a model to the table and then you replace it. What do you think the setModel(...) method does?


And by the way, the question is in no way related to the other posting you posted in. You just made a silly mistake. And that is why you don't hijack other postings because each question is different.

And use the code tags. Did you not notice how the code in the other posting was nicely foramtted?
Archana Honnavalli
Ranch Hand

Joined: Feb 26, 2008
Posts: 39
Rob Camick wrote:
I guess the model object has these rows but its not reflected in jTable1 object.


Because you have two TableModels. What do you think the setModel(...) method does?


And by the way, the question is in no way related to the other posting you posted in. You just made a silly mistake. And that is why you don't hijack other postings because each question is different.

And use the code tags. Did you not notice how the code in the other posting was nicely foramtted?


So you mean in jTable1 there is a DefaultTableModel?
If so how do i add rows dynamically in jTable1 object itself.
Any method i can use that helps me add rows from a list??

I am new to Swings.
Thus an explanation about the solution is appreciated.

Also please explain me why setModel should be used?

Thanks,
Archana
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10255
    
    8

Archana,

I am new to Swings.

To start with it is Swing and not Swings
http://faq.javaranch.com/java/SwingIsAProperNoun

Did you read Rob's reply properly? He had asked you
Because you have two TableModels. You add a model to the table and then you replace it. What do you think the setModel(...) method does?




Line #1 - You create a table model instance
Line #2 - You create a table using the model above
Line #3 - You replace the existing model with another model instance
Line #4&5 - You are manipulating data in the old model which is no longer used by your table because of #3

Do you see the problem now?

Since you mentioned you are new to Swing, it might be a good idea to go through this
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
Also, like Rob mentioned, please use the code tags in future, while posting code.
http://faq.javaranch.com/java/UseCodeTags



[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
santhosh varala kumar
Greenhorn

Joined: Nov 05, 2009
Posts: 25
Steps can we follow
1. Create a table Model with given column names and zero rows
2. Create a JTable instance with table model as parameter.
3. Then Dynamically adding rows can be done by using addRow() method.

---

If your trying to use different models for the same table. Then in that case we can go for the setModel method concept.
---
To visible colNames in JTables , you need to put JTable inside a jScrollPane.

For More about Jtable and its corresponding model. Read JAVA API..,.


Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10255
    
    8

Santosh,

I have edited your post to remove the quotes as it made your post unnecessarily long and a bit of a problem to read.
You can always use the button to reply.
mark goking
Ranch Hand

Joined: Aug 18, 2009
Posts: 155
archana, JTable is based on the MVC architecture. it is the view.

if you want to manipulate data like adding rows, the TableModel should be the one that needs to be manipulated

Website/Java Games: http://www.chitgoks.com
Tech Blog: http://tech.chitgoks.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to add rows to jTable dynamically?