R
radvieira
Hi everyone
I've been trying to delete a row from my JTable without complete
success. My problem is that while the row is removed from both the
model and the view, the view is showing the wrong value - the value
from the deleted row. Here are the steps to reproduce my issue:
1. Display a single row in a JTable with all cells populated with data.
2. Click a button which adds a new row to the JTable's model.
3. A new empty row is displayed at index 0 and the existing row is
positioned at index 1.
4. Enter data into a cell in the new row and keep focus in that cell.
5. Click a button which removes the row at index 0.
6. The new row is removed from the model and the view, but the existing
row's column now takes on the value of the removed row's edited cell.
Below is the relevent code:
code:
public class AddNewAction extends AbstractAction {
private ReservationTableModel model;
private boolean isNew;
public AddNewAction(String name, Icon icon, ReservationTableModel
model) {
super.putValue(Action.NAME, name);
super.putValue(Action.SMALL_ICON, icon);
this.model = model;
}
public void actionPerformed(ActionEvent event) {
AbstractButton source = ((AbstractButton)event.getSource());
if (!isNew) {
this.model.insertRow(0);
source.setText("Undo");
isNew = true;
} else {
this.model.removeRow(0);
source.setText( ((String)super.getValue(Action.NAME)) );
isNew = false;
}
}
}
public class ReservationTableModel extends AbstractTableModel {
private List columnNames = new Vector();
private List data = new Vector();
public ReservationTableModel(Vector data, Vector columnNames) {
this.data.addAll(data);
this.columnNames.addAll(columnNames);
}
...other methods
public Vector insertRow(int row) {
Vector newRow = new Vector(this.columnNames.size());
for (int i = 0; i < this.columnNames.size(); i++) {
newRow.add(null);
}
this.data.add(0, newRow);
fireTableRowsInserted(row, row);
return newRow;
}
public void removeRow(int row) {
this.data.remove(row);
fireTableRowsDeleted(row, row);
}
...other methods
}
I'm not sure of what else I need to be doing here in order to get this
working.
Any help provided is appreciated as always.
Thanks
Raul
I've been trying to delete a row from my JTable without complete
success. My problem is that while the row is removed from both the
model and the view, the view is showing the wrong value - the value
from the deleted row. Here are the steps to reproduce my issue:
1. Display a single row in a JTable with all cells populated with data.
2. Click a button which adds a new row to the JTable's model.
3. A new empty row is displayed at index 0 and the existing row is
positioned at index 1.
4. Enter data into a cell in the new row and keep focus in that cell.
5. Click a button which removes the row at index 0.
6. The new row is removed from the model and the view, but the existing
row's column now takes on the value of the removed row's edited cell.
Below is the relevent code:
code:
public class AddNewAction extends AbstractAction {
private ReservationTableModel model;
private boolean isNew;
public AddNewAction(String name, Icon icon, ReservationTableModel
model) {
super.putValue(Action.NAME, name);
super.putValue(Action.SMALL_ICON, icon);
this.model = model;
}
public void actionPerformed(ActionEvent event) {
AbstractButton source = ((AbstractButton)event.getSource());
if (!isNew) {
this.model.insertRow(0);
source.setText("Undo");
isNew = true;
} else {
this.model.removeRow(0);
source.setText( ((String)super.getValue(Action.NAME)) );
isNew = false;
}
}
}
public class ReservationTableModel extends AbstractTableModel {
private List columnNames = new Vector();
private List data = new Vector();
public ReservationTableModel(Vector data, Vector columnNames) {
this.data.addAll(data);
this.columnNames.addAll(columnNames);
}
...other methods
public Vector insertRow(int row) {
Vector newRow = new Vector(this.columnNames.size());
for (int i = 0; i < this.columnNames.size(); i++) {
newRow.add(null);
}
this.data.add(0, newRow);
fireTableRowsInserted(row, row);
return newRow;
}
public void removeRow(int row) {
this.data.remove(row);
fireTableRowsDeleted(row, row);
}
...other methods
}
I'm not sure of what else I need to be doing here in order to get this
working.
Any help provided is appreciated as always.
Thanks
Raul