• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

add an empty row to the Jtable  RSS feed

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody,
I have a Jtable and i am going to add an empty row after pressing a button named insert.
Whenever i put these following code in my class ,it works properly.

I should add after these 2 lines i create a JScrollPane and add it to the getContentPane().

THE PROBLEM:
Whenever i put these 2 lines in a seperate function like addRow() and call this function after pressing the insert button , i see no changes in Jtable,i mean a new empty row isn't added to the table.
I think i should add a new table to the JScrollPane but since i'm a new programmer in java ,i don't know how to do this.

Can anyone help me please?
 
Ranch Hand
Posts: 176
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

hany hashemi wrote:
Whenever i put these following code in my class ,it works properly.

...



Where exactly in the class are you putting these instruction?

hany hashemi wrote:
Whenever i put these 2 lines in a seperate function like addRow() and call this function after pressing the insert button , i see no changes in Jtable



Why wouldn't it? If it works elsewhere in the class I don't see why it wouldn't work inside a method like addRow().

Post more of your code. An example when it works, and an example when it doesn't.
 
hany hashemi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This part of code took a long time and i will be appreciative if you can help me solving my problem.
Thanks in advance.
 
Sheriff
Posts: 21424
94
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because you're creating a new JTable that does not share the TableModel. You then wrap that JTable in a JScrollPane and add that to the center of the content pane, removing the previous component.
 
hany hashemi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry i didn't get it.
You mean i should delete the following lines from the code:

so i just have these lines in the insertRow() function:

but it still doesn't work properly yet...

Would you please tell me what should i do now?
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He meant, you need to get an existing TableModel from the JTable. For example I have used DefaultTableModel (suggesting you to consider it to) and when I want to add a new row (when there are no more empty rows in the table) to the table I get its existing Model and add the new row to it:

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Author
Ranch Hand
Posts: 964
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

hany hashemi wrote:
You mean i should delete the following lines from the code:

so i just have these lines in the insertRow() function:

but it still doesn't work properly yet...



Almost. You need to delete the model = new DefaultTableModel(data,col) line also. (You want to add a row to the existing table model, not instantiate a separate table model.)

Of course you need to ensure your model variable gets initialized (one time only) to the actual table model you are using before calling insertRow().
 
hany hashemi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help.
I did what you suggested and removed a code which was declaring a new model while adding a new row.But it still doesn't work.I get so frustrated and completely confused. :!:
This is a new part of my code:
 
Brian Cole
Author
Ranch Hand
Posts: 964
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

hany hashemi wrote:Thanks for your help.
I did what you suggested and removed a code which was declaring a new model while adding a new row.But it still doesn't work.I get so frustrated and completely confused. :!:
This is a new part of my code:



You are still calling model = new DefaultTableModel(data,columnNames) each time the user clicks the button, right? Stop doing that.
 
hany hashemi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I put it outside of the method.I mean before pressing the insert button ,i declare a model and inside the insertRow i just add row.BUT ... ???!!!
evrything is just like before,without any changes...
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you seem to have missed the point of everyone - get rid of the 'new' table model.

probably easier to give you a working example and have you try to follow it


 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Michael Dunn I used your code for my project
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!