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.
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
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..,.