简体   繁体   中英

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

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

您必须使用System.out.println(Array.toString(//您的代码在这里));

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

}

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