brent_watson
asked on
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)myJTab le.getMode l(); myAbsModel.fireTableDataCh anged(); //(OR myAbsModel.fireTableRowsUp dated(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.cl one(); // Copy new list into Array linked to JTable
myData[0][0] = "Test Change";
AbstractTableModel myAbsModel= (AbstractTableModel)myJTab le.getMode l(); myAbsModel.fireTableDataCh anged();
...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.
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)myJTab
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.cl
myData[0][0] = "Test Change";
AbstractTableModel myAbsModel= (AbstractTableModel)myJTab
...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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Exellent. Worked like a charm. Thanks.
:-)
>>use javax.swing.table.DefaultT ableModel as your table model
You were already doing so ;-)
>>use javax.swing.table.DefaultT
You were already doing so ;-)
with this method you can change the data array
setDataVector(Object[][] dataVector, Object[] columnIdentifiers)