Prev: Calling .NET exec from Java
Next: configure Spring to use jax-ws endpoint to consume XML then produce JSON
From: Marcin Rodzik on 7 May 2010 04:42 Being aware of what has been said http://ow.ly/1I50n http://ow.ly/1I50J I'd like to ask explicitly: what should I do if I want to display some information to the user in a Swing GUI app using a JDialog? I know dialogs are event-driven so that execution of the thread which requested displaying the dialog stops, but other currently fired events are still handled. The thing I don't know and I'd like to is: should I create a Runnable object and schedule it for execution: SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { /* display the dialog here */ } }); or: I can simply write the code like this: JOptionPane.showMessageDialog(parent, msg, title, messageType); without the SwingUtilities stuff and Swing will handle the code correctly anyway? I used to follow the first option but now I doubt if it makes any sense. regards, Marcin
From: Lew on 7 May 2010 10:55 Marcin Rodzik wrote: > Being aware of what has been said <http://ow.ly/1I50nhttp://ow.ly/1I50J> > I'd like to ask explicitly: what should I do if I want to display some > information to the user in a Swing GUI app using a JDialog? I know > dialogs are event-driven so that execution of the thread which > requested displaying the dialog stops, but other currently fired > events are still handled. > > The thing I don't know and I'd like to is: should I create a Runnable > object and schedule it for execution: > > SwingUtilities.invokeAndWait(new Runnable() { > @Override > public void run() { > /* display the dialog here */ > } > > }); > Usually the JDialog is raised in response to an event already on the dispatch thread, but using invokeAndWait() or invokeLater() is fine if you're already on the EDT. I don't know why you'd prefer invokeAndWait() over invokeLater(). > or: > > I can simply write the code like this: > > JOptionPane.showMessageDialog(parent, msg, title, messageType); > > without the SwingUtilities stuff and Swing will handle the code > correctly anyway? > Only if you're already on the EDT to start with, otherwise it's a bad idea. > I used to follow the first option but now I doubt if > it makes any sense. > Depends on whether you really mean for the caller to wait for the dialog. -- Lew
From: John B. Matthews on 7 May 2010 12:25 In article <1cc05fba-94dd-4ee1-8e67-0e04d303cb7f(a)i9g2000yqi.googlegroups.com>, Marcin Rodzik <marteno_rodia(a)o2.pl> wrote: > I know dialogs are event-driven so that execution of the thread which > requested displaying the dialog stops, but other currently fired > events are still handled. Perhaps I misunderstand, but this seems incorrect. A modal dialog blocks the caller's ability to post new user generated events to the event dispatch thread (EDT), but the EDT itself continues to process events. Based on this clarification, <http://groups.google.com/group/comp.lang.java.programmer/msg/c6ddf50e2b6e1fa1> this program would seem to be a counter-example: <http://groups.google.com/group/comp.lang.java.programmer/msg/d95da1f8f9af2339> > I can simply write the code like this: > > JOptionPane.showMessageDialog(parent, msg, title, messageType); > > without the SwingUtilities stuff and Swing will handle the code > correctly anyway? I would advocate this simpler approach, subject to the caveats adduced by Lew in a related response. -- John B. Matthews trashgod at gmail dot com <http://sites.google.com/site/drjohnbmatthews>
From: Daniel Pitts on 10 May 2010 17:24 On 5/7/2010 1:42 AM, Marcin Rodzik wrote: > Being aware of what has been said http://ow.ly/1I50n http://ow.ly/1I50J > I'd like to ask explicitly: what should I do if I want to display some > information to the user in a Swing GUI app using a JDialog? I know > dialogs are event-driven so that execution of the thread which > requested displaying the dialog stops, but other currently fired > events are still handled. > > The thing I don't know and I'd like to is: should I create a Runnable > object and schedule it for execution: > > SwingUtilities.invokeAndWait(new Runnable() { > @Override > public void run() { > /* display the dialog here */ > } > }); > > or: > > I can simply write the code like this: > > JOptionPane.showMessageDialog(parent, msg, title, messageType); > > without the SwingUtilities stuff and Swing will handle the code > correctly anyway? I used to follow the first option but now I doubt if > it makes any sense. SwingUtilities is old, use java.awt.EventQueue instead. Yes, if you are not on the dispatch queue, you should use invokeAndWait or invokeLater. -- Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
From: Daniel Pitts on 10 May 2010 17:28 On 5/7/2010 7:55 AM, Lew wrote: > Marcin Rodzik wrote: >> Being aware of what has been said<http://ow.ly/1I50nhttp://ow.ly/1I50J> >> I'd like to ask explicitly: what should I do if I want to display some >> information to the user in a Swing GUI app using a JDialog? I know >> dialogs are event-driven so that execution of the thread which >> requested displaying the dialog stops, but other currently fired >> events are still handled. >> >> The thing I don't know and I'd like to is: should I create a Runnable >> object and schedule it for execution: >> >> SwingUtilities.invokeAndWait(new Runnable() { >> @Override >> public void run() { >> /* display the dialog here */ >> } >> >> }); >> > > Usually the JDialog is raised in response to an event already on the > dispatch thread, but using invokeAndWait() or invokeLater() is fine if > you're already on the EDT. > > I don't know why you'd prefer invokeAndWait() over invokeLater(). invokeAndWait must *not* be called from the EDT. One would prefer invokeAndWait if the caller wants to be sure the event is fully processed before continuing. > Depends on whether you really mean for the caller to wait for the > dialog. Which is a common (if misguided) idiom. In view code, it makes sense, but if you put it in model/controller code, you are likely to run into huge problems later. -- Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
|
Next
|
Last
Pages: 1 2 Prev: Calling .NET exec from Java Next: configure Spring to use jax-ws endpoint to consume XML then produce JSON |