JTableModel行添加错误

时间:2018-07-25 12:58:11

标签: java swing

这是我的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;
    }
}

这是我添加新条目的方式:

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");
    }
}

,这里是addScheduledRecord方法:

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;
}

如果我添加一个条目,则将“上一个”添加到表中,但是,如果我关闭表并再次打开它,那都是正确的。 RecorderController只是拥有一个自己的列表用于其他目的(此列表以另一种方式修改) 有人在这里看到我的错误吗?

1 个答案:

答案 0 :(得分:1)

这应该修复addRow方法(有容器方法时无需迭代):

public void addRow(ScheduledRecord r) {
    if (scheduledRecords.contains(r)) {
        throw new IllegalArgumentException("Scheduled Record " + r.toString() + " already exists");
    }
    scheduledRecords.add(r);
}