Jerry
Jerry

Reputation: 420

JTable not refreshing after removing rows

I have my Jtable inside a ScrollPane which in-turn is inside a Panel. So Whenever I remove a row using

model.removeRow(row_number);

it does remove the row(verified by calling model.getRowCount()) but the view remains the same.

public void createTable(){
    model = new DefaultTableModel(rowData, columnNames);
    table = new JTable(model);

    table = new JTable(rowData, columnNames);
    table.setEnabled(false);
    table.setShowVerticalLines(false);
    scrollPane_2 = new JScrollPane(table);
    scrollPane_2.setBounds(10, 16, 554, 89);
    scrollPane_2.setBorder(BorderFactory.createEmptyBorder());
    panel_6.add(scrollPane_2);

}

I have tried revalidate(), repaint() on all the three (panel, table and scrollpane), also

model.fireTableDataChanged();

but still the view dosen't change...

Upvotes: 0

Views: 173

Answers (1)

bradimus
bradimus

Reputation: 2523

You're throwing away the JTable based on your model when you reassign the variable table.

table = new JTable(model);

table = new JTable(rowData, columnNames);

When you remove the row from model, the JTable you are displaying never hears about it since it has its own model.

Upvotes: 5

Related Questions