Assuming you have the following classic combination of ...
*) a JTable,
*) a TableSorter and
*) a TableModel
Furthermore the data of your TableModel is frequently changing like this:
TableModel:
public void addRecord(Record Record) {
data.add(record);
fireTableDataChanged();
}
The invocation of fireTableDataChanged() notifies all listeners that the
model has changed. If a TableSorter is linked between the model and the
view it will receive the notification.
My question is:
If you are changing the model (as shown in the example method above) it
has to be done one AWT-event-thread, right? If the model is frequently
changed, do I have to wait until one event is completely processed thus
changing the model step by step, waiting each time until each listener
has reacted to the change.
I ask this question because I have some nasty problems. Exceptions like
ArrayOutOfBoundsException in TableSorter or exceptions while iterating
through the data container of the model, all occurring on the
AWT-event-thread. I can imagine that the following things happen:
1. Method addRecord is invoked, changing data in the container of the
model and notifying listeners.
2. While notifying the listeners of the former change to the model the
method addRecord is invoked again changing the data. Listeners like
TableSorter stumble because they process a former notification with data
of a latter change.
Could that be?
Thanks for any help,
Bernhard
> public void addRecord(Record Record) {
> data.add(record);
> fireTableDataChanged();
> }
Why are you not using fireTableRowsInserted?
(assuming a record consists of a rows).
> The invocation of fireTableDataChanged() notifies all listeners that the
> model has changed. If a TableSorter is linked between the model and the
[quoted text clipped - 3 lines]
> If you are changing the model (as shown in the example method above) it
> has to be done one AWT-event-thread, right? If the model is frequently
Yes.
> changed, do I have to wait until one event is completely processed thus
> changing the model step by step, waiting each time until each listener
> has reacted to the change.
What do you mean by "one event is completely processed"?
> I ask this question because I have some nasty problems. Exceptions like
> ArrayOutOfBoundsException in TableSorter or exceptions while iterating
[quoted text clipped - 6 lines]
> 2. While notifying the listeners of the former change to the model the
> method addRecord is invoked again changing the data. Listeners like
How is that supposed to happen (if the event-dispatch thread is notifying
the listeners, how can at the same time addRecord be invoked)?
Christian
Bernhard Pauler - 13 Jan 2005 13:27 GMT
> What do you mean by "one event is completely processed"?
EventQueue.invokeAndWait(Runnable runnable)
> How is that supposed to happen (if the event-dispatch thread is notifying
> the listeners, how can at the same time addRecord be invoked)?
If another thread would deliver new data. But since the addRecord(...)
method with its call fireTableRowsInserted should only be invoked by the
AWT-event-thread this should not happen. You are right. Thank you.