• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

How to add rows to jTable dynamically?

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
I wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic