Updating JTable Atomically - block data changes while updating.

Discussion in 'Java' started by news.rcn.com, Dec 9, 2007.

  1. news.rcn.com

    news.rcn.com Guest

    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
    news.rcn.com, Dec 9, 2007
    #1
    1. Advertising

  2. news.rcn.com

    Daniel Pitts Guest

    news.rcn.com wrote:
    > 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.

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Dec 9, 2007
    #2
    1. Advertising

  3. news.rcn.com

    Roedy Green Guest

    On Sun, 9 Dec 2007 11:29:03 -0500, "news.rcn.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.

    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Dec 10, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Enigma Curry
    Replies:
    1
    Views:
    486
    Peter Hansen
    Mar 15, 2006
  2. Tintin92
    Replies:
    1
    Views:
    1,702
    Andrew Thompson
    Feb 14, 2007
  3. Chris Thomasson
    Replies:
    42
    Views:
    2,000
    Casper H.S. Dik
    Sep 22, 2007
  4. Chris Thomasson
    Replies:
    35
    Views:
    2,148
    Chris M. Thomasson
    Aug 1, 2008
  5. wrex
    Replies:
    2
    Views:
    149
Loading...

Share This Page