Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

Updating JTable Atomically - block data changes while updating.

Thread view: 
news.rcn.com - 09 Dec 2007 16:29 GMT
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
Daniel Pitts - 09 Dec 2007 19:13 GMT
> How can I have a JTable update (in response to a TableModelEvent) and
> prevent the data model from changing during the update?
[quoted text clipped - 18 lines]
>
> 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-concurr
ency-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.

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Roedy Green - 10 Dec 2007 11:49 GMT
On Sun, 9 Dec 2007 11:29:03 -0500, "news.rcn.com"
<cant_jim@hotmail.com> wrote, quoted or indirectly quoted someone who
said :

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

you would need to make methods or code blocks synchronized to prevent
two threads for executing that code simultaneously.

See http://mindprod.com/jgloss/synchronised.html
http://mindprod.com/jgloss/thread.html

This is much trickier than you might first suppose.  You will likely
want to read some books.

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.