{
// If a table cell is being edited, we must cancel the editing
// before deleting the row.
if (cookieTable.isEditing())
{
TableCellEditor cellEditor =
cookieTable.getCellEditor(
cookieTable.getEditingRow(),
cookieTable.getEditingColumn());
cellEditor.cancelCellEditing();
}
int rowSelected = cookieTable.getSelectedRow();
if (rowSelected != -1)
{
tableModel.removeRow(rowSelected);
tableModel.fireTableDataChanged();
// Disable the DELETE and SAVE buttons if no rows remaining
// after delete.
if (tableModel.getRowCount() == 0)
{
deleteButton.setEnabled(false);
saveButton.setEnabled(false);
}
// Table still contains one or more rows, so highlight
// (select) the appropriate one.
else
{
int rowToSelect = rowSelected;
if (rowSelected >= tableModel.getRowCount())
{
rowToSelect = rowSelected - 1;
}
cookieTable.setRowSelectionInterval(
rowToSelect,
rowToSelect);
}
}
}
}
else if (action.equals("Add"))
{
// If a table cell is being edited, we should accept the current
// value and stop the editing before adding a new row.
if (cookieTable.isEditing())
{
TableCellEditor cellEditor =
cookieTable.getCellEditor(
cookieTable.getEditingRow(),
cookieTable.getEditingColumn());
cellEditor.stopCellEditing();
}
tableModel.addNewRow();
tableModel.fireTableDataChanged();