Kaushik Balasubramanain
Kaushik Balasubramanain

Reputation: 1258

Delete a row from JTable

I have a table model. I want to delete a row from a table. I am not able to find the model.removeRow method. Can someone tell he how to remove a row? Below is the code for the model.

class TableModel extends AbstractTableModel {
private Object[][] data;
private String[] columnNames;

public TableModel(Object[][] data) {
    this.data = data;
    columnNames=new String[1];
    columnNames[0]="Data";
}

public int getRowCount() {
    return data.length;
}

public int getColumnCount() {
    return columnNames.length;
}

public Object getValueAt(int rowIndex, int columnIndex) {
    return data[rowIndex][columnIndex];
}

public Class getColumnClass(int c) {
    return String.class;
}

public boolean isCellEditable(int rowIndes, int columnIndex) {
    return false;
}

public String getColumnName(int index) {
    return columnNames[index];
}

public void setValueAt(Object value, int rowIndex, int columnIndex) {
    data[rowIndex][columnIndex] = value;
    fireTableCellUpdated(rowIndex, columnIndex);
}

}

Upvotes: 0

Views: 3279

Answers (4)

DejanLekic
DejanLekic

Reputation: 19787

Kaushik, if you really want to make your own implementation of the TableModel interface and deal with the storage of rows manually (as you obviously do by using Object[][] data), then you must implement the removeRow() method because abstract table model does not "know" how to do it. removeRow() will have to fireTableRowsDeleted() at the end...

A good alternative is to refactor your class and make it extend the DefaultTableModel.

Another suggestion - since table model changes a lot, a dynamic array may be a bad choice for storage because you must either reallocate space every time you add a row (bad), or you preallocate enough space for certain number of rows, then whenever there is no enough space you allocate new Object[][] with some extra space (for next N rows). Typically people allocate enough space for 2^n rows during the reallocation process.

Upvotes: 1

Kaushik Balasubramanain
Kaushik Balasubramanain

Reputation: 1258

DefaultTableModel is one solution. But if yo still want to use abstrac table model, this is how you do.

This is the new model definition.

class TableModel extends AbstractTableModel {

private Object[][] data;
private String[] columnNames;

public TableModel(Object[][] data) {
    this.data = data;
    columnNames=new String[1];
    columnNames[0]="Data";
}

public int getRowCount() {
    return data.length;
}

public int getColumnCount() {
    return columnNames.length;
}

public Object getValueAt(int rowIndex, int columnIndex) {
    return data[rowIndex][columnIndex];
}

public Class getColumnClass(int c) {
    return String.class;
}

public boolean isCellEditable(int rowIndes, int columnIndex) {
    return false;
}

public String getColumnName(int index) {
    return columnNames[index];
}

public void removeRow(int row)
{
    Object [][]newData=new Object[data.length-1][data[0].length];
    for(int i=0;i<newData.length;i++){
        for(int j=0;j<data[0].length;j++){
            newData[i][j]=data[i+1][j];
        }
    }
    data=new Object[newData.length][columnNames.length];
    data=newData;
    fireTableRowsDeleted(row, row);
}


public void setValueAt(Object value, int rowIndex, int columnIndex) {
    data[rowIndex][columnIndex] = value;
    fireTableCellUpdated(rowIndex, columnIndex);
}

}

You first need to remove the row data from the data object. and then remove the row.

Upvotes: 2

BuZz
BuZz

Reputation: 17465

you must be missing something that is obvious in the default model, maybe you're mot overriding properly if you're doing your own and you might thing it's missing :

DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);

// Create some data
model.addColumn("Col1");
model.addRow(new Object[]{"r1"});
model.addRow(new Object[]{"r2"});
model.addRow(new Object[]{"r3"});

// Remove the first row
model.removeRow(0);

// Remove the last row
model.removeRow(model.getRowCount()-1);

Upvotes: 2

StanislavL
StanislavL

Reputation: 57381

Use DefaultTableModel. It has public void removeRow(int row) method.

Upvotes: 4

Related Questions