File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes add an empty row to the Jtable Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "add an empty row to the Jtable" Watch "add an empty row to the Jtable" New topic
Author

add an empty row to the Jtable

hany hashemi
Greenhorn

Joined: Dec 17, 2008
Posts: 14
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?
Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

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.


Olly
hany hashemi
Greenhorn

Joined: Dec 17, 2008
Posts: 14


This part of code took a long time and i will be appreciative if you can help me solving my problem.
Thanks in advance.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19232

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.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
hany hashemi
Greenhorn

Joined: Dec 17, 2008
Posts: 14
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?
denis sorn
Ranch Hand

Joined: Apr 30, 2008
Posts: 33
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:

Mikko Kohtamäki
Ranch Hand

Joined: Dec 13, 2008
Posts: 112
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 852
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().


bitguru blog
hany hashemi
Greenhorn

Joined: Dec 17, 2008
Posts: 14
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

Joined: Sep 20, 2005
Posts: 852
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

Joined: Dec 17, 2008
Posts: 14
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...
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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


Dinesh Jayram
Greenhorn

Joined: Nov 26, 2008
Posts: 23
Thanks Michael Dunn I used your code for my project
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: add an empty row to the Jtable
 
Similar Threads
JTable ........
Vector and JTable
JTable
How to set size of columns in JTable
jtable