• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 389
  • Last Modified:

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.
0
brent_watson
Asked:
brent_watson
2 Solutions
 
hoomanvCommented:
use javax.swing.table.DefaultTableModel as your table model
also use table.setModel(dataModel) to change the model
0
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now