Update JTable using a multidimensional array

I've been Googling for an answer for about an hour with no luck, so I thought I would see if anybody has some insight for me...

I have a program, wherein I create a JTable using the constructor that takes a multidimensional array of data and an array of column headings

I.E.:
Object[][] myData = {{"test1", "test1"}, {"test2", "test2"}};
String[] colHeadings = {"Col 1", "Col 2"};
myJTable = new JTable(myData, colHeadings);

The array (myData) contains a list of files from a directory.  What I am trying to accomplish is changing the array and having the changes reflect in the JTable.  Where I am getting stuck is if the new data is longer than the old (array held list of 2 files now has 3 files).

I.E.:
If I do this, JTable displays my changes as I want:
myData[0][0] = "Test Change";
AbstractTableModel myAbsModel= (AbstractTableModel)myJTable.getModel();            myAbsModel.fireTableDataChanged();  //(OR myAbsModel.fireTableRowsUpdated(x, y);  works too

But, if I need to change the size of the array, which will move the reference to a different object, this stops changes from reflecting in myJTable

I.E.: ...This dosen't to work! :-( ...
Object[][] updatedList = getFileList(); // Returns Object[][] of data for files
myData = (Object[][])updatedList.clone();  // Copy new list into Array linked to JTable
myData[0][0] = "Test Change";
AbstractTableModel myAbsModel= (AbstractTableModel)myJTable.getModel();            myAbsModel.fireTableDataChanged();

...Now, hopfully I didn't make something easy sound confusing...

Can anyone suggest a better way to update the data in myJTable with new data from a different array (that could be different size? [More records, no change in #columns]).

Thanks in advance.
brent_watsonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hoomanvCommented:
use javax.swing.table.DefaultTableModel as your table model
also use table.setModel(dataModel) to change the model
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hoomanvCommented:
DefaultTableModel is capable of data resizing
with this method you can change the data array
setDataVector(Object[][] dataVector, Object[] columnIdentifiers)
0
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

CEHJCommented:
Using your own code, all you need to do is this:


Object[][] myData = {{"test1", "test1"}, {"test2", "test2"}};
String[] colHeadings = {"Col 1", "Col 2"};
myJTable = new JTable(myData, colHeadings);

...

Object[][] updatedList = getFileList(); // Returns Object[][] of data for files

DefaultTableModel dtm = (DefaultTableModel)myJTable.getModel();
dtm.setDataVector(updatedList, colHeadings);
dtm.fireTableStructureChanged();
0
brent_watsonAuthor Commented:
Exellent.  Worked like a charm.  Thanks.
0
CEHJCommented:
:-)

>>use javax.swing.table.DefaultTableModel as your table model

You were already doing so ;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.