• Post Reply Bookmark Topic Watch Topic
  • New Topic

Jtable Update

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody tell how to write code in the table model so that my jtable gets updated.
Thanks,
Raja
 
Joe Guzzardo
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Define these as class variables:

JTable jTable;
TableMap tableMap;

Add this class to JApplet:

class TableMap extends AbstractTableModel {
String[][] lists;

public TableMap(String[][] l) {
lists = l;
}

public void newTable(String[][] l) {
lists = l;
}

public Object getValueAt(int aRow, int aColumn) {
return lists[aRow][aColumn];
}

public int getRowCount() {
return(lists.length);
}

public int getColumnCount() {
return(lists[0].length);
}

}


Then in my applet I added this code:

tableData = (String[][])vector.elementAt(0); // or whatever you use to get the string array


if (tableMap == null) {
System.out.println("about to creat new map");
tableMap = new TableMap(tableData);
jTable = new JTable(tableMap);
jTable.setRowHeight(17);

System.out.println("created new map");
} else {
System.out.println("about to update old map");
tableMap.newTable(tableData);
jTable.setModel(tableMap);
System.out.println("updated old map");
}

Finally make sure you add this line of code after performing the above if statement:

getContentPane().repaint();

This worked for me. It took me about 4 days to get it to work right, my problem was that if I added new data to the JTable the old data would reappear when the screen was refreshed.

Hope this helps. Regards, Joe





 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!