简体   繁体   中英

How to convert JTable Vector to JTable ArrayList?

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));
    }

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM