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.
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
posted
0
This part of code took a long time and i will be appreciative if you can help me solving my problem.
Thanks in advance.
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.
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:
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().
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
posted
0
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
posted
0
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...