Tuytuy
Tuytuy

Reputation: 169

getting value from JTable to an array

Hey I was trying to get the values from a JTable to an array and then print it. I seems like it is actually taking the adress of something instead of taking the value. I don't understand why. Here is my code:

public Object[][] getTableData (JTable table) {
    DefaultTableModel dtm = (DefaultTableModel) table.getModel();
    int nRow = dtm.getRowCount();
    int nCol = dtm.getColumnCount();
    Object[][] tableData = new Object[nRow][nCol];
    for (int i = 0 ; i < nRow ; i++){
        for (int j = 0; j < nCol ; j++)
        tableData[i][j] = dtm.getValueAt(i,j);
    }
    System.out.println(Arrays.asList(tableData));
    return tableData;
}

Upvotes: 2

Views: 925

Answers (3)

Ihraz
Ihraz

Reputation: 48

DefaultTableModel model = new javax.swing.table.DefaultTableModel();

    model.addColumn("Col1");
    model.addColumn("Col2");

    model.addRow(new Object[]{"1", "v2"});
    model.addRow(new Object[]{"2", "v2"});

    List<String> numdata = new ArrayList<String>();
    for (int count = 0; count < model.getRowCount(); count++){
          numdata.add(model.getValueAt(count, 0).toString());
    }

    System.out.println(numdata);

try this

Upvotes: 3

Ihraz
Ihraz

Reputation: 48

List< Object> list = new ArrayList< Object>();

for (int i = 0 ; i < nRow ; i++){

    for (int j = 0; j < nCol ; j++)
     list.add(dtm.getValueAt(i,j).toString());  

}

Upvotes: 0

Super deamon
Super deamon

Reputation: 36

you have to use System.out.println(Array.toString(//your code here));

Upvotes: 0

Related Questions