Reputation: 2369
this is my TableModel:
public class ScheduledRecordsTableModel extends AbstractTableModel {
private String[] headers = {"Interpret", "Titel"};
private List<ScheduledRecord> scheduledRecords;
public ScheduledRecordsTableModel(List<ScheduledRecord> recordsList) {
super();
this.scheduledRecords = recordsList;
}
@Override
public int getRowCount() {
return scheduledRecords.size();
}
@Override
public int getColumnCount() {
return 2;
}
@Override
public String getColumnName(int column) {
return headers[column];
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
ScheduledRecord r = scheduledRecords.get(rowIndex);
switch (columnIndex) {
case 0:
return r.getActor();
case 1:
return r.getTitle();
default:
return null;
}
}
public void addRow(ScheduledRecord r) {
ScheduledRecord toAdd = null;
for (Iterator<ScheduledRecord> recordIterator = scheduledRecords.iterator(); recordIterator.hasNext(); ) {
ScheduledRecord record = recordIterator.next();
if (record.equals(r)) {
throw new IllegalArgumentException("Scheduled Record " + r.toString() + " already exists");
} else {
toAdd = record;
}
}
scheduledRecords.add(toAdd);
}
public void removeRow(ScheduledRecord r) {
scheduledRecords.remove(r);
fireTableDataChanged();
}
public ScheduledRecord getScheduledRecordFromIndex(int index) {
return scheduledRecords.get(index);
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
}
here is how i add new entries:
private void saveScheduledRecord() {
if (checkInputValues()) {
WebradioPlayer.addScheduledRecord(new ScheduledRecord(titleField.getText(), artistField.getText()));
this.dispose();
} else {
JOptionPane.showMessageDialog(this, "Please enter a valid artist/title", "Invalid input", JOptionPane.ERROR_MESSAGE);
throw new IllegalArgumentException("artist or title input did not match the specifications");
}
}
and here the addScheduledRecord method:
public static synchronized boolean addScheduledRecord(ScheduledRecord record) {
RecorderController.getInstance().addScheduledRecord(record);
Gui.getInstance().getRecorderTab().getScheduledRecordsWindow().getTable().getScheduledRecordsTableModel().addRow(record);
Gui.getInstance().getRecorderTab().getScheduledRecordsWindow().getTable().getScheduledRecordsTableModel().fireTableDataChanged();
databaseConnector.addScheduledRecord(record);
return true;
}
If i add an entry, the 'previous' one is added to the table, however if i close the table and open it again it is all correct. RecorderController just holds an own list for other purposes (this list is modified in another way) Does anyone see my mistake here?
Upvotes: 0
Views: 44
Reputation: 4223
This should fix the addRow method (no need to iterate, while there is a contains method):
public void addRow(ScheduledRecord r) {
if (scheduledRecords.contains(r)) {
throw new IllegalArgumentException("Scheduled Record " + r.toString() + " already exists");
}
scheduledRecords.add(r);
}
Upvotes: 1