Hello, there I have got a problem about JTable. I used DefaultTableModel to create a table, and the table is displayed properly. When I try to add or delete a row, it just does not update my table. Here is the code:
Yo u can see that I try many ways to update my table, but all failed.So I suspect that : either addRow(), removeRow() do not cause any effect to the tablemodel, or I have not found the right way to update the change of the table.
The above message was posted by me last time.Someone is kind enough to isolate the code and make a little program to test it, and it works nicely, thank you very much for that. Unfortunately, I have not fixed the problem, that means I fail to add rows or delete rows(the table does not show any change). Maybe I need to give more details for my problem:
1. The structure of the frame in which my table is : lower_panel.add(new JScrollPane(myTable), "Center");
middle_panel.add(lower_panel, "Center");
getContentPane().add(middle_panel,"Center");
I was wondering, do I need to update(or repaint) the lower_panel(parent panel of table) whenever I add rows or delete rows to / from my table? I tried that using myTable.repaint(), lower_panel.repaint(), or repaint(1), but all failed. I also tried to use remove(Component comp) to remove table and lower_panel and add the updated ones, but not successful.
2. I also tried to test whether I did change tha table whenever I add rows. For example: I do the following when I press the AddRow button in the actionPerformed():
addRow(temp); // temp is 2-d array int index1 = myTable.getRowCount(); String v = (myTable.getValueAt(index1-1,0)).toString(); new MessageDialog(FrameObj.this, "Now the count of row is " + index1 + ", and the added value is " + v);
I can see that the rowcount is increased and the added value, they are all correct. But the appearance of the table does not change a bit. So I think the problem maybe relate to the display stuff,like the parent panel of table(lower_panel) does not get updated or something else does not get updated that I dont know.
3. The frame in which table is gets displayed by choosing a menuitem in the my main class, I dont know whether that caused the problem. Actually, I made a little program to test that(create a button in the main class, so the table will be brought up when the button is pressed), and everything went fine.
I am just getting desperate, hope someone can help me. Thank you very much in advance .
Actually, my problem is the addRow() is working, because the count of row changes when I add a row , and I can retrive the added data from the added row using getValueAt(). But the table does not show the added row.I have tried all the way to update the table, but it still does not work.
Try calling validate() method on table just after you add the row. I had this problem before. It is just paiting problem. If it doesn't work then post your complete code. Someone will help you out.