Swing Model Classes Updating Swing Components on a Thread Other Than AWT

Discussion in 'Java' started by mkrause, May 6, 2005.

  1. mkrause

    mkrause Guest

    Having built several Java swing applications in the past, I am well
    aware of the potential for problems if you update a Swing component on
    a thread other than the AWT thread. Knowing this, I have been careful
    to use Runnable + SwingUtilities.invokeLater (or
    SwingUtilities.invokeAndWait) where it's needed, or I'll use the
    SwingWorker class to perform a bunch of non-GUI related work in the
    construct and then udpate my GUI components in the finished method.

    I think an assumption I have been making, however, has gotten me into
    trouble. Let me try to illustrate the problem:

    If you have a model object I'll call M which is created on a server and
    then passed to the client via Java RMI. Once on the client, M can be
    accessed from a variety of different threads. Now assume that some GUI
    component such as a JList indirectly gets notified when something in M
    changes. (I say indirectly because the class that actually receives
    the property change from M is the ListModel that in turn notifies the
    JList through a ListModel event.)

    Now let's say that this property of interest changes, so M fires the
    property change from a thread other than the AWT thread. The
    PropertyChangeSupport inside of this model class will fire the change
    by walking through its listener list and making method calls to all
    objects that were interested in the change. As mentioned before, one
    of the things that cares about this property change is a ListModel.
    The ListModel will receive this property change on the same thread and
    then fire off a different event that will be received by a JList
    component - again on the SAME thread - and... voila... we have a GUI
    component being updated by a thread other than the AWT thread. Not

    So, the issue really is to not assume DefaultListModel or
    DefaultTableModel, etc., will make sure to fire their changes on the
    AWT thread, because they won't. There is no special code in these
    classes to ensure that this is done. The onus to do so falls on the
    application developer.

    Has anyone else come to this realization, and if so, how did you deal
    with this problem? I have searched for other posts related to this
    problem but haven't found much.

    - Mike
    mkrause, May 6, 2005
    1. Advertisements

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. Miss Elaine Eos

    AWT -> Swing, blocked dispatch thread

    Miss Elaine Eos, Jan 4, 2004, in forum: Java
    Miss Elaine Eos
    Jan 5, 2004
  2. Ryan Phelps

    Very busy Swing/AWT thread

    Ryan Phelps, Jul 7, 2004, in forum: Java
    Roedy Green
    Jul 8, 2004
  3. dolphin

    Swing better than Awt?

    dolphin, Mar 18, 2007, in forum: Java
    Andrew Thompson
    Mar 29, 2007
  4. Randyl
    Roedy Green
    Jul 25, 2007
  5. Sourabh Mhaisekar

    AWT or Swing or Any other ?

    Sourabh Mhaisekar, Dec 7, 2012, in forum: Java

Share This Page