Jump to content
  • Advertisement
Sign in to follow this  
ace84_84

JTables

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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?).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!