Reputation: 1
How can I convert JTable
Vector
to JTable
ArrayList
? I changed all vector to arraylist, but I see some issue in calling DefaultTableModel
. I have problem in converting the ArrayList
to 2D array. Please help.
Here is my code:
String headers[] = {"Pick Columns"};
rows = new ArrayList<String>();
columns = new ArrayList<String>();
addColumns(headers, columns);
tabModel = new DefaultTableModel(convertTowDArray(rows.toArray()), headers);
table = new JTable(tabModel);
Here are the methods:
private Object[][] convertTowDArray(Object[] toArray) {
Object[][] o = null;
for (int i = 0; i < toArray.length; i++) {
o[i][0] = toArray[i];
}
return o;
}
public void addColumns(String[] colName, List<String> col)// Table Columns
{
col.addAll(Arrays.asList(colName));
}
Upvotes: 0
Views: 1091
Reputation: 205885
Instead of trying to convert existing data into the form prescribed by the DefaultTableModel
constructors, extend AbstractTableModel
and let your implementation of getValueAt()
access your data structure(s) directly. Simple examples are shown here, here and here.
Upvotes: 1