Swing development tip: Migrate event listeners when setting models

Change this -

public void setTableModel(TableModel tableModel) {
    this.tableModel = tableModel;
}

to this -

public void setTableModel(TableModel tableModel) {
    //migrate listeners
    if(this.tableModel != null) {
        if(this.tableModel.getTableModelListeners() != null) {
            for (TableModelListener p : this.tableModel.getTableModelListeners()) {
                tableModel.addTableModelListener(p);
            }
        }
    }
    this.tableModel = tableModel;
}

No comments:

Post a Comment