简体   繁体   English

JTableModel行添加错误

[英]JTableModel rows wrong added

this is my TableModel: 这是我的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: 这里是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;
}

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? RecorderController只是拥有一个自己的列表用于其他目的(此列表以另一种方式修改),有人在这里看到我的错误吗?

This should fix the addRow method (no need to iterate, while there is a contains method): 这应该修复addRow方法(无需迭代,尽管有一个contains方法):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM