The Artima Developer Community
Sponsored Link

Java Answers Forum
Table still does not get updated

3 replies on 1 page. Most recent reply: Jun 28, 2004 7:07 PM by Kishori Sharan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Sam Li

Posts: 5
Nickname: sam2004
Registered: Jun, 2004

Table still does not get updated Posted: Jun 27, 2004 8:24 PM
Reply to this message Reply
Advertisement
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:

class tabletest extends JFrame {


String dataValues[][] = new String[4][2];
String columnNames[] = {"tag name", "description"};

/* some codes are missing here ; they are for buttons, textfield etc.*/

/* use for loops to fill in dataValues[][].*/

DefaultTableModel tableModel = new DefaultTableModel(dataValues, columnNames);
JTable myTable = new JTable(tableModel);


lower_panel.add (new JScrollPane(myTable), "Center");


}


The above code can display the table, but when I try to add a row or remove a row , it does not work(table is not updated.)


These codes are somewhere in the class tabletest:

add_row_button.addActionListener (new ActionListener () {
public void actionPerformed(ActionEvent e) {

String temp[] = {"a new row", " "};


DefaultTableModel t = (DefaultTableModel) myTable.getModel();
t.addRow(temp);
//t.fireTableChanged(null);
t.fireTableData Changed();
//myTable.invalidata;
//myTable.validate;
//myTable.repaint();
//myTa ble.validate();
//myTable.invalidate();
//myTable.tableChanged();
myTable.repaint();
}
}

Codes for removing a row:

remove_row_button.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent e) {

int index = table.getSelectedRow();
if (index != -1)
{

((DefaultTableModel)table.getModel()).removeRow(index);

}


}

});


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 .

Sam


mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Table still does not get updated Posted: Jun 27, 2004 10:48 PM
Reply to this message Reply
I can't guess exactly what problem u are facing.

Did u try

fireTableRowsInserted(int firstRow,int lastRow)
or fireTableRowsDeleted(int, int) methods?

Also try putting ur addRow code in a try/catch block to see if any exception is being thrown or not?

Sam Li

Posts: 5
Nickname: sam2004
Registered: Jun, 2004

Re: Table still does not get updated Posted: Jun 27, 2004 11:47 PM
Reply to this message Reply
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.

Sam

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Table still does not get updated Posted: Jun 28, 2004 7:07 PM
Reply to this message Reply
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.

Flat View: This topic has 3 replies on 1 page
Topic: Socket Prog Previous Topic   Next Topic Topic: Processing large files suggestions

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use