Sign in to follow this  

JTables

This topic is 4354 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
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

This topic is 4354 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this