Java Custom AbstractTableModel doesn't update values on JTable till Window/JTable resize

226 views Asked by At

So i have my AbstractTableModel:

public class ClientTableModel extends AbstractTableModel {

private String[] columns = new String [] {
        "UserNames", "Passwords"
};

@Override
public int getRowCount() {
    return ServerManager.instance.allClients.size();
}

@Override
public int getColumnCount() {
    return columns.length;
}

@Override
public Class<?> getColumnClass(int columnIndex) {
    return String.class;
}

@Override
public String getColumnName(int column) {
    return columns[column];
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
    Object value = "";
    Client client = ServerManager.instance.allClients.get(rowIndex);
    switch (columnIndex) {
        case 0:
            value = client.CLIENT_USERNAME;
            break;
        case 1:
            value = client.CLIENT_PASSWORD;
            break;
    }
    return value;
}
}

When i update the 'allClients' with a new client the JTable's row with the new client doesn't appear unless I resize the JTable.

I was wondering if it is possible for the JTable to update with the new client row without me having to manually resize the window to see the new client.

1

There are 1 answers

0
Hovercraft Full Of Eels On BEST ANSWER

I don't see your model's addRow(...) method, but I'm guessing that you're adding data to the nucleus of the table model's data, the ServerManager.instance.allClients singleton without firing any of the table model's notification methods. If so, don't do this. The nucleus of the model should be part of your model itself, and whenever adding or removing or changing data, you must call a fireXxxx(...) notification method. For instance, whenever you add a row of data, you must call fireTableRowsInserted(...) in your model so that it notifies any tables that display it.