# JTables

This topic is 4430 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have been searching google and yahoo and i havent been able to find what i was looking for, so i hope someone will be able to help me. say I have a JTable named table i am making a program where the user clicks something, then the table get fill up with the data that corresponds to that object selected, then if the user clicks another object, the tables data then changes to the newly selected object's data. My problem is that i dont know how to update the table so that the old data is cleared and the new data shows up. When i create the table it looks like this. rows, cols are the data. JTable table = new JTable(rows, cols); is there a way to do this????

##### Share on other sites
There doesn't seem to be an easy way to repopulate the table, so just create a new table with your row/col data every time the user hits the button. There probably is a way to update the current Jtable object's data instead of creating a new one... but it isn't obvious to me from scanning the JTable doc.

By the way, there's a Java forum that will probably be a better place to post Java questions in the future. [smile]

##### Share on other sites
What you can do is inherit from javax.swing.table.AbstractTableModel, and give your own TableModel as an argument to JTable.

I believe (but it's been a while) that the default table model is DefaultTableModel (hence the name [wink]). You could try doing:
  if( myTable.getModel instanceof DefaultTableModel ) {     DefaultTableModel model = (DefaultTableModel)myTable.getModel();     model.getDataVector().clear();     model.addRow( myRowData );  }

However, that may be tricky (who guarantees the JTable will always have a DefaultTableModel as its model?).