That old chestnut: Swing Threading - EventQueue

First of all, I'm sorry if this topic has been done to death already but I really have searched through the forums and tutorials and not found a solution that I can get to work.
Scenario (the usual):
- Swing GUI application
- Button fires off 'IntensiveProcess'
- Display a 'MessageBox' while 'IntensiveProcess' is running
Problem:
MessageBox is not displayed (or is displayed but its contents are not painted - depending on which [incorrect] implementation I try) until IntensiveProcess has finished.
I have tried all kinds of different ways of accomplishing this but I can't for the life of me get it to work:
The implementations I have tried involved various combinations of:
- EventQueue.invokeLater()
- EventQueue.invokeAndWait()
- inner and annonymous-inner Runnable classes
As far as I understand it, the following strategy should work:
public class MySwingApp extends JFrame
    //create button with Action: DoButtonAction
    private class DoButtonAction extends AbstractAction
        public void actionPerformed(ActionEvent e)
            // ~~~ IMPLEMENTAION 1  ~~~
            EventQueue.invokeLater(new ShowMessageBox());
            EventQueue.invokeLater(new IntensiveProcess());
            // ~~~ IMPLEMENTAION 2 ~~~
            EventQueue.invokeLater(new ShowMessageBox());
            //cannot call invokeAndWait() from Event Dispatcher thread so:
            (new Thread(new Runnable()
                public void run()
                    EventQueue.invokeAndWait(new IntensiveProcess());
            )).start();
            // ~~~ IMPLEMENTAION 3  ~~~
            EventQueue.invokeLater(new IntensiveProcess());
            //cannot call invokeAndWait() from Event Dispatcher thread so:
            (new Thread(new Runnable()
                public void run()
                    EventQueue.invokeAndWait(new ShowMessageBox());
            )).start();
            //dispose of message box if user didn't close it
    private class ShowMessageBox implements Runnable
        //display MessageBox...
    private class IntensiveProcess implements Runnable
        //execute intensive process...
}None of the above 3 implementations yield the correct result.
Can anyone please point out where I'm going wrong?
Thanks all.
John

I have implemented your suggestion but sadly I am
getting the same result: the entire GUI hangs until
IntensiveProcess has completed, and only then is the
message box displayed.Hmm, with the intenstive process in its own thread (construct() runs in a thread other than EDT) the GUI shouldn't hang like that, I'm not sure why that is. Maybe try invokeAndWait instead. Or, did you try displaying the message box before creating the SwingWorker? Make sure the message box is not modal if you create it before creating SwingWorker.
I can't grasp why the EventDispatcher thread is
getting tied up with IntensiveProcess. I thought
calling EventQueue.invokeLater() (or SwingWorker) is
supposed allow the executing thread (in my case the
EventDispatcher thread) to continue. So why is the
GUI not getting updated and the message box being
displayed until AFTER IntensiveProcess finishes?As I understand it, invokeLater means stuff this Runnable into the event queue and run it when its turn comes up, while invokeAndWait runs the Runnable immediately. Neither method will wait for another thread to finish like you're expecting, that's what SwingWorker does.
For some background info, the SwingWorker's construct() method creates a new non-EDT thread, which is where you're supposed to do time-intensive background work, and the code inside the finished() method will be run in the EDT, which is where you're supposed to update Swing components. The major benefit is the happens-before guarantee, construct() will run to completion before finished() is invoked. Maybe you already knew that, but it's important to know so I wanted to make sure.

Similar Messages

  • That old chestnut: *** Security Sandbox Violation *** clogging the debug trace

    Hi,
    I am attempting to debug a project for Flash Mobile and getting very irritated by *** Security Sandbox Violation *** warnings whenever I move, touch or interact with the application in any way whatsoever. These typically fire two or three _PAGES_ of warnings for a simple drag operation... Because there are so many of these warnings, the debug trace is effectively redundant and useless unless I actively insert a manual break point immediately after the traces I want to view - fine for a comms operation, not so useful when attempting to debug coordinates for a drag operation.
    The cause, obviously, is that I have loaded external content for display in a SWFLoader component...
    Because I am in AIR for mobile, I cannot access Security to allow the domain and crossdomain from the content provider is ignored or not loaded.
    I have found a number of references to this ranging from very recent to several years ago, such as this one which indicates a) the trace is a bug and b) something was being done about it: http://forums.adobe.com/thread/576999
    I cannot find any reference to a fix ever being implemented and the trace issue is driving me nuts.
    Please, please, please can somebody direct me to a workaround to hide this? I've tried all kinds of disabling for mouse events, keyboard events, mouse children etc.
    Thanks,
    Overwhelmed-in-the-traces of Dublin

    Is it bad form to bump?
    G

  • Iphoto 4.03 failing to start - that old chestnut!

    I have had the common problem whereby I tried to start iphoto, only to be faced with the 1st page and loading pictures message, but no photos.
    Old Toad and Brie Fly have both suggested alternative solutions on a number of occasions both of which I have used succesfully. What puzzles me is that twice today I have burnt photos to CD or DVD and after each time, iphoto has failed to load so I have had to resort to rebuilding the library and on each occasion this has taken many attempts. Does anyone know why after burning CDs it may do this?
    imac   Mac OS X (10.4)  

    today I have burnt photos to CD or DVD and after each
    time, iphoto has failed to load so I have had to
    resort to rebuilding the library and on each occasion
    this has taken many attempts.
    This just happened to me. My iphoto 4.03 library became corrupted after burning a cd for second half of my entire library (total 2700 photos). After burning a CD, I noticed that the CD came up in the iphoto album/library view as a library and iphoto used it when I went to view my recent photos as if it were the main library. I should have noticed something was weird!
    After exiting iphoto and later restarting it, iphoto took a long time to load photos and then said it had recovered some photos. After a lot of time trying to figure what happened I noticed that these recovered photos where the entire first half of my iphoto library which wasn't burnt to cd!
    Upon each iphoto restart, it would go through the recovered photos again making a new Recovered_photos album each time. And again, and again.
    I rebuilt the Library which fixed the repeated recovery problem, but I lost all roll info.

  • Export to word - that old chestnut.

    Hey all,
    I imported a word document to pages, changed it with tracking enabled, saved it as .pages then exported it as .doc
    Now the people that I needed to read it can't. I have two (non computer literate) PC users on the line with MS Office. I sent them the file - apparently it gets halfway through loading then hangs. I've tried on Mac MS Office and it works fine (the only reason I opened it in pages in the first place is because of Microslag's slow-*** adoption of Universal Binary - it often doesn't open at all for me). I've tried various ideas - saving again from office etc. to no avail.
    Anyone know why this isn't working?
    Edd

    today I have burnt photos to CD or DVD and after each
    time, iphoto has failed to load so I have had to
    resort to rebuilding the library and on each occasion
    this has taken many attempts.
    This just happened to me. My iphoto 4.03 library became corrupted after burning a cd for second half of my entire library (total 2700 photos). After burning a CD, I noticed that the CD came up in the iphoto album/library view as a library and iphoto used it when I went to view my recent photos as if it were the main library. I should have noticed something was weird!
    After exiting iphoto and later restarting it, iphoto took a long time to load photos and then said it had recovered some photos. After a lot of time trying to figure what happened I noticed that these recovered photos where the entire first half of my iphoto library which wasn't burnt to cd!
    Upon each iphoto restart, it would go through the recovered photos again making a new Recovered_photos album each time. And again, and again.
    I rebuilt the Library which fixed the repeated recovery problem, but I lost all roll info.

  • Credit card update issues.....that old chestnut :(

    I have been trying to update my credit card details via Adobe.com but to no avail. There is NO LINK......no edit details ...nothing!! I sign in using the correct info and navigate to my products and services > scroll to my subscriptions and the only clickable text is 'manage'. So I go ahead and click away and it takes me to Subscription details where it tells me my photoshop/ Illustrator subscription is processing and in red text tells me to go update my credit card details as my card is expired. I would if I could and have tried  for the past few days. There is no 'edit billing' link to click. I have been on the phone with customer service for the woman to tell me 'try again in a few days there is nothing I can do.' I have sat on the online customer service for what seems like forever for it to keep popping up and telling me I have one person ahead of me and it may take 2mins and 47 sec to get to a service person. Longest 2mins 47sec I have ever experienced.
    Adobe all I want to do is update my card details so I can pay you money to use your unreal software...it cant be that hard can it???
    Please tell how me I can do this before my products cease to work!

    Hi Purpledragaon,
    I'll report the Edit Billing option being missing for you. I'm sorry for the inconvienence.
    -Dave

  • Killing a Swing thread

    Hello,
    How can I kill a Swing thread? For example the program below would never exit. Sure you can type System.exit(0) which would exit anything, but what are the other solutions?
    I tried putting chooser = null;
    System.gc();in the end of main() but this won’t work.
    To formulate my question better - how would I kill a thread I do not have complete command of (i.e. I can’t do usual do/while thread stopping)?
    Big thanks in advance.
    public class SwingLiveTest{
        public static void main(String[] args) {
            JFileChooser chooser = new JFileChooser();
            if (chooser.showSaveDialog(null) != JFileChooser.APPROVE_OPTION) return;
            File file = chooser.getSelectedFile();
            System.out.println(file);
            /*chooser = null;
            System.gc(); */

    You can't kill any Thread like this, at least not those where there is no method to call.
    It is possible to cause a Thread to stop, call the stop method, however this is not the recommended approach for very good reasons (Read the JavaDocs for java.lang.Thread.stop() method to see why.
    It should be possible, once you have obtained a reference to the Thread you wish to stop, to call interrupt() on that Thread.
    This is not guaranteed to work because you are dependent on the implementation of the Thread class you are manipulating or the Runnable that the Thread instance is currently running.
    There may be classes in the com.sun packages which can aid you with this problem, this is where Sun usually sticks undocumented implementation however these packages are not officially supported and there is no guarantee that classes you use in one VM are available in another, in particular when the VM you are using is not a Sun VM.

  • Swing Threads

    Does anybody know how the threads in a swing application are organized. I noticed, that when i start a swing application more then 10 threads are created. Furthermore, when the "main" thread is terminated after showing the main window (a JFrame window), two windows are displayed. When the main-thread is ran in a loop (while (true) {try{Thread.sleep(100);}catch(Exception x){}), only one window is displayed. So, even if the main thread does nothing, it's necessary.
    Furthermore, i realized that, when starting other threads out from the main thread, on some win 2000 systems the threads are properly terminated but the associated handles are not freed - this leads to a "handle leak" after a certain time (48 hours). If the threads are started with invokeLater out from the main thread, then the handles are freed properly when the thread terminates.
    Does anybody know how these facts can be put together to get a bigger whole ?
    Thanks a lot.
    Stephan Gloor
    Switzerland

    hi,
    the rule is:
    swing components are not thread-safe except some, and therefore they should only be changed from within the event dispatch thread. this can be accomplished using the methods invokeLater(Runnable) and invokeAndWait(Runnable) in class javax.swing.SwingUtilities.
    best regards, Michael

  • I cant sync my iphone to my re installed itunes.  It is saying my iphone is already synced with another library.  How do I get rid of that old Itunes library that I no longer have access too?

    I recently re installed itunes (due to pc crashing).  When I connect my iphone to sync it is telling me my Iphone is already synced to another library.  I no longer have access to that library.  How do I delete that old library to enable me to sync to my new library?

    You don't need to delete the old library, but it will take a little effort to transfer the allegiance of the device to your new one.
    You should be able to:
    Backup the device to the new installation
    Transfer your purchases into a newly authorized library
    Recover any other media using third party tools as suggested in this post from forum regular Zevoneer
    Restore the device from the backup
    This process should switch the association of the device to the new library, preserving the data and settings in your apps.
    May also be prudent to attempt a backup to iCloud directly from the device before you start... Settings > iCloud > Storage & Backup > iCloud Backup > On. It won't save any media but it would preserve the general account settings and documents in case anything goes wrong while you are trying to extract your other data.
    When you get it all fixed, make a backup!
    tt2

  • I have an old apple id that i havent used for years. For some reason i have updates on my mac i cannot do unless i sign onto that old account. It uses an old e mail account i no longer use or have access to. How can i get to do these updates?

    I have an old apple id that i havent used for years. For some reason i have updates on my mac i cannot do unless i sign onto that old account. It uses an old e mail account i no longer use or have access to. How can i get to do these updates?

    its very specific.
    If you are seeing the old Apple ID with regard to this Mac, then it would seem that somehow you have incorporated the ID in the setup of this new Mac.
    Iphoto i use never used imovie.
    If you began using iPhoto without having Accepted it into your Apple ID, then that would explain why you are unable to update the app. If any of the iLife apps are used without Accepting them into your Apple ID, the ability to accept them will lapse and you will not be able to update them.

  • I have intel mac  10.411, want to access iTunes, won't let me unless upgrade tp itunes 10, but it needs OSX 10.5. I just want to download some music, not upgrade my operating system. It's not THAT old. Ideas?

    I have intel mac  10.411, want to access iTunes, won't let me unless upgrade tp itunes 10, but it needs OSX 10.5. I just want to download some music, not upgrade my operating system. It's not THAT old. Ideas?

    As long as you don't need all the bells and whistles of the iTunes Store such as Ping, and don't need to sync an iOS device running iOS 4.x, then iTunes 9.2.1 should work for you. It should be available via Software Update, but if not, you can still get it here:
    http://support.apple.com/kb/DL1056
    but it would be best to upgrade your Mac to Mac OS X 10.5 or 10.6, since it's probable that eventually the iTunes Store will no longer support iTunes 9.x.
    Regards.
    P.S. Mac OS X 10.4 was discontinued three and a half years ago, so yes, it's getting rather long in the tooth. Cheers.

  • In this case, can I modify swing GUI out of swing thread?

    I know the Swing single thread rule and design (Swing is not thread safe).
    For time-consuming task, we are using other thread to do the task and
    we use SwingUtilities.invokeAndWait() or SwingUtilities.invokeLater (or SwingWorker) to update Swing GUI.
    My problem is, my time-consuming task is related to Swing GUI stuff
    (like set expanded state of a huge tree, walk through entire tree... etc), not the classic DB task.
    So my time-consuming Swing task must executed on Swing thread, but it will block the GUI responsivity.
    I solve this problem by show up a modal waiting dialog to ask user to wait and
    also allow user to cancel the task.
    Then I create an other thread (no event-dispatch thread) to do my Swing time-consuming tasks.
    Since the modal dialog (do nothing just allow user to cancel) is a thread spawn by
    Swing event-dispatch thread and block the Swing dispatch-thread until dialog close.
    So my thread can modify Swing GUI stuff safely since there are no any concurrent access problem.
    In this case, I broke the Swing's suggestion, modify Swing stuff out of Swing event-dispatch thread.
    But as I said, there are no concurrent access, so I am safe.
    Am I right? Are you agree? Do you have other better idea?
    Thanks for your help.

    If you drag your modal dialog around in front of the background UI, then there is concurrent access: paint requests in your main window as the foreground window moves around.

  • Is my ipod that old?

    I have a second generation iPod touch, 8gb. Is it just that old that I cant update it? I have games on it from a couple of years ago as well as music but thats about it. Can I trade it in with apple or what should I do? I mean I can just use it for music otherwise, its useless. The apps on it dont stay open for more than a couple of minutes and I cant download anything because itunes or the app store wont open.

    That's just wonderful… I have an ipod given when I purchased my second Mac after first one failed when 1tb Seagate hard drive took a crap. Now I find out that the second Mac needs hard drive replaced because too or it may take a crap. So now I find out that my three year old ipod is nothing but a paperweight!? My son tried to use it for school and it will not support any apps. I have a two year old $600 ipad that is giving me problems with not supporting apps. You know how I found out? I updated my kindle reader as advised and now I cant even get to the 200 books i purchased. Kindle fire is much more affordable if this is how it works!

  • I am trying to update my apps. When it prompts for my password my old deleted email address comes up. I don't even rember the password for that old email. How can I get to update my apps with my current email?

    I am trying to update my apps. When it prompts for my password my old deleted email address comes up. I don't even rember the password for that old email. How can I get to update my apps with my current email?

    All apps are tied to the Apple ID they are purchased with and is not transferable.
    Delete app and download app with your new Apple ID.

  • HT201269 My old iPhone (I no longer have) is still an active device on my iCloud backup list.  Is it safe to delete it?  Can I be sure everything on the cloud for that old device has been transferred?

    My old iPhone (I no longer have) is still an active device on my iCloud backup list.  Is it safe to delete it?  Can I be sure everything on the cloud for that old device has been transferred?

    The only way the backup from ilcoud has been transferred to a new apple device would be if YOU restored the new device with the mentioned backup. There is nothing 'safe' about you deleting this backup if in fact you have not restored the new device with this backup.
    Good luck Friend.

  • Changed Apple ID email Ok,however old email still linked to iCloud lost that login password and can't retrieve or change it because that old email address is not active anymore.

    Hi all,
    I had to change ISP and therefore I had to change my email address.
    Did that easily for my Apple ID for both iTunes, iPhone and iPad.
    However the email address for the iCloud somehow stayed attached to my old dead email account.
    I can't delete the account off the iPad for some reason it still comes up with the old email address for the Apple ID.
    I can't get an email sent to rectify it as it's dead! I can't reset it as my current Apple ID password is incorrect for that old email address.
    Any ideas on how to rectify the issue?
    Thanks.

    If the old ID is yours, and if it is an earlier version of your current ID, go to https://appleid.apple.com, click Manage my Apple ID and sign in with your current iCloud ID.  Click edit next to the primary email account, change it back to your old email address and save the change.  Then edit the name of the account to change it back to your old email address.  You can now use your current password to turn off Find My iDevice, even though it prompts you for the password for your old account ID. Then save any photo stream photos that you wish to keep to your camera roll.  When finished go to Settings>iCloud, tap Delete Account and choose Delete from My iDevice when prompted (your iCloud data will still be in iCloud).  Next, go back to https://appleid.apple.com and change your primary email address and iCloud ID name back to the way it was.  Now you can go to Settings>iCloud and sign in with your current iCloud ID and password.

Maybe you are looking for

  • How can I get caller id to announce the caller from my contact list?

    My old Motorolla and LG phones had a setting to speak the caller id before ringing.  It took the names from the contact list or if not there would just call out the phone number and then go to the ringtone.  Does the Iphone 4 not have this setting? 

  • Infopath submit button only works for the first row in a repeating table‏

    Hi, I have a InfoPath Email submit button issue I could not figure out. I have a master/detail repeating table in my form, and one of fields is "EmailAddress" which shows customer's email address. I created a submit button and put the "EmailAddress"

  • SD Card not mounting (SanDisk Ultra 32gb)

    Another MacBook Pro Problem Hi, I am trying to insert my 32GB SanDisk Ultra SD card into my MacBook Pro 15", however it isnt reading the card, and nothing happens. I have tried restarting the Mac, updated all software/hardware and still nothing. Does

  • WEB Proxy issue after Mountain Lion Update

    I had MAC OS X 10.6.8 on  my Mac mini before I updated it with MAC OS X 10.8. To connect to Internet we have some proxy setting in our company for which secure web proxy server is mentioned for web proxy and secure web proxy options in advanced setti

  • Apple Magic Mouse and kernel panics?

    I bought the new Apple Magic Mouse a few days ago and since day one I am getting random kernel panics. I've switched back to my old Apple Might Mouse and everything seems to work just fine. I am using a Macbook Pro 2.4MHz Intel Core 2 Duo, 4GB 1067MH