Updating JTable Atomically - block data changes while updating.

N

news.rcn.com

How can I have a JTable update (in response to a TableModelEvent) and
prevent the data model from changing during the update?

I am getting ArrayIndexOutOfBounds exception during update and I believe
this is because the application is deleting rows during the update. I have
used a lock to mutually exclude the application from changing the data model
and the JTable from reading the data; the latter locks the getRowCount(),
getColCount(), and getValueAt() methods. The problem, I believe, is that a
row gets deleted between the time getRowCount() is called and all the
getValueAt()s have been made; the index in error is always one more than the
# of rows.

Is there a way I can have the JTable do the update atomically? That is, make
all it's calls with as a single transaction?

I can't use a more granular TableModelEvent, to specify the rows to update
because the application does not know where in the table the information
will appear. Also, it is not the last row that is being deleted.

Thanks in advance for your help.

jim cant
 
D

Daniel Pitts

news.rcn.com said:
How can I have a JTable update (in response to a TableModelEvent) and
prevent the data model from changing during the update?

I am getting ArrayIndexOutOfBounds exception during update and I believe
this is because the application is deleting rows during the update. I have
used a lock to mutually exclude the application from changing the data model
and the JTable from reading the data; the latter locks the getRowCount(),
getColCount(), and getValueAt() methods. The problem, I believe, is that a
row gets deleted between the time getRowCount() is called and all the
getValueAt()s have been made; the index in error is always one more than the
# of rows.

Is there a way I can have the JTable do the update atomically? That is, make
all it's calls with as a single transaction?

I can't use a more granular TableModelEvent, to specify the rows to update
because the application does not know where in the table the information
will appear. Also, it is not the last row that is being deleted.

Thanks in advance for your help.

jim cant
The trick is to do everything on the Event Dispatch Thread.

Look up EventQueue.invokeLater(). I also suggest reading the book Java
Concurrency In Practice.
<http://virtualinfinity.net/wordpress/technical-book-recommendations/java-concurrency-in-practice/>.

It explains all aspects of dealing with a multi-threaded program. It
may be a little known fact, but most non-trivial Java applications are
multi-threaded. For instance, Swing GUI programs are always multi-threaded.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top