Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By doWhile

Thread: Displaying a JDialog from a thread other than the EDT

  1. #1
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Displaying a JDialog from a thread other than the EDT

    It's late and I can't get my head around this problem.

    My application uses a worker thread that may need to display a dialog. Simple enough to queue a Runnable to be invoked by the EDT. But how can I get the user's input back from the dialog in a thread-safe way?

    The simplest solution I can think of would be to use a bean with synchronized getters and setters. First the worker thread would create the bean, populate its fields, and use it to construct the dialog in the EDT. Then the dialog would update the bean when it is closed. Finally, the worker thread would retrieve the new values from the bean. The worker thread would wait() on the bean, and the dialog would notify() it.

    Does anyone know a better pattern? Meanwhile, I'm going to sleep on it.
    Get in the habit of using standard Java naming conventions!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: Displaying a JDialog from a thread other than the EDT

    SwingPropertyChangeSupport with a PropertyChangeListener?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Displaying a JDialog from a thread other than the EDT

    Another alternative - construct a bean which contains fields for the values you need, create a final instance of this bean, then call SwingUtilities.invokeAndWait to display the dialog and populate the bean values. The thread will wait until the Swing Runnable is complete (or an Exception is thrown), after which your bean is populated with the values.
    kjkrum likes this.

Similar Threads

  1. jDialog box disappears
    By pink123 in forum AWT / Swing
    Replies: 3
    Last Post: 08-19-2011, 06:07 PM
  2. JDialog
    By Nuclear Mosquito in forum AWT / Swing
    Replies: 1
    Last Post: 07-06-2010, 03:27 PM
  3. JDialog
    By frenk_castle in forum AWT / Swing
    Replies: 3
    Last Post: 05-11-2010, 12:28 PM
  4. setLocation on a JDialog is ignored
    By ScottVal in forum AWT / Swing
    Replies: 7
    Last Post: 01-13-2009, 07:35 AM
  5. help with jdialog
    By leonard in forum AWT / Swing
    Replies: 1
    Last Post: 08-05-2007, 05:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •