0

I'm quite new in Java, so please don't be so hard if this question is very stupid.

I have a problem. I want to display incoming messages in a list. So I choose a JTable, with an AbstractTableModel and a static LinkedList.

The initialization of the Table looks like this:

jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jTable1.setModel(new MessagesTableModel());
jScrollPane1.setViewportView(jTable1);

My AbstractTableModel looks like this:

public class MessagesTableModel extends AbstractTableModel {
private static List<MsgGeneral> data;
// Specify the types of columns. -> constructor
Class[] types;
private static String[] COLUMN_NAMES = {
    "Timestamp",
    "Length",
    "Direction",
    "Header",
    "Content"
};

public MessagesTableModel() {
    //define column types
    types = this.types = new Class[]{
        String.class,
        Integer.class,
        String.class,
        Integer.class,
        Object.class};

    data = MainThread.MessageListGeneral;
}

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

@Override
public Class getColumnClass(int columnIndex) {
    return types[columnIndex];
}

@Override
public int getRowCount() {
    return data.size();
}

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

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
    return false;
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {

    MsgGeneral msg = data.get(rowIndex);

    switch (columnIndex) {
        case 0:
            return msg.getTimestamp();
        case 1:
            return msg.getMsgLenght();
        case 2:
            return msg.getDirection();
        case 3:
            return msg.getHeader();
        case 4:
            return msg.getContent().toString();
        default:
            return null;
    }
}

The List is simply defined like this:

public static LinkedList<MsgGeneral> MessageListGeneral = new LinkedList<MsgGeneral>();

And I fill this list like this:

MessageListGeneral.add(new MsgGeneral(data.length, 0, header, "some note", data));

And now my Question, or what I don't understand: How can I dynamically refresh the table, when a new row is added? I think I need something like a ObservableCollection in .NET, or a event listener on the "add". How can I do that.

Thanks a lot for every hint br Alex

  • http://stackoverflow.com/questions/3179136/jtable-how-to-refresh-table-model-after-insert-delete-or-update-the-data – HectorLector Feb 01 '14 at 22:18
  • I would strongly recommend Glazed Lists for this type of problem, it takes care of a LOT of the work for you and provides a thread-safe way to do this really quite easily. http://www.glazedlists.com/documentation/tutorial – caprica Feb 02 '14 at 08:40

1 Answers1

4

When you call MessageListGeneral.add it needs to fire some kind of notification, telling registered listeners that a new message is available.

These listeners then need to react to that event appropriatly. In your case, you would need to determine the row(s) that were added and use fireTableRowsInserted, which will notify the JTable that it should repaint.

This is a basic observer pattern

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366