简体   繁体   中英

Only last row is shown in JTable

I need the table to display all the rows, however it only displays the last low.

for (i = 0; i < links.size(); i++) {
    Object row[] = {links.get(i).getAttribute("href"), statusCode};
    final Object rowData[][] = {row};
    final Object columnNames[] = {"Link", "Status Code"};
    DefaultTableModel listTableModel = new DefaultTableModel(rowData, columnNames);
    resultTable.setModel(listTableModel);
}

What do I do? I'm not so good with programming. Still learning.

While looping through your rows you are creating a new DefaultTablemodel for each row and then replacing the previous one when doing resultTable.setModel() . But the TableModel should contain the data for the entire table. So create one TableModel before your loop and add each row like this:

//create TableModel
final Object columnNames[] = {"Link", "Status Code"};
DefaultTableModel listTableModel = new DefaultTableModel(columnNames,0);
//fill the TableModel
for (i = 0; i < links.size(); i++) {
    Object row[] = {links.get(i).getAttribute("href"), statusCode};
    listTableModel.addRow(row)
}
//associate the model with the table
resultTable.setModel(listTableModel);

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