This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Cannot call invokeAndWait, can't invokeLater either Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Cannot call invokeAndWait, can Watch "Cannot call invokeAndWait, can New topic
Author

Cannot call invokeAndWait, can't invokeLater either

Sam Smith
Greenhorn

Joined: May 31, 2005
Posts: 17
Hi all,

I'm writing a small class that part of a much larger application that I can't modify. The class is supposed to open a java program and return a JPanel representing the program's output window. That JPanel will then display in the main program's JFrame.

Here is a snippet of my code:

This code works fine if I create a little main method in this class which gets this JPanel and sticks in in a new JFrame. Nothing wrong there.

However, when the main program tries to get the JPanel to place it in the JFrame, I get:

Where the line referenced at the bottom of that is


Reading through some tutorials, I see that if getJPanel() were called using InvokeLater, the problem should be fixed(?). However, I can't change the code that calls this method. I tried putting the code above in a Runnable, and calling InvokeLater on it, but I get the same error.

Anyone have any clues as to how I can fix this, without changing either the code that calls this method or the WaveInterferenceApplication?

Any help would be much appreciated,
Thanks!
Sam
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18141
    
  39

Anyone have any clues as to how I can fix this, without changing either the code that calls this method or the WaveInterferenceApplication?


Nope. To fix this you must change the code that calls the method. Basically, the invokeAndWait() method call is trying to dispatch the runnable to the event dispatching thread and then wait for it to finish.

However, obviously, it is not possible to do this from the event dispatching thread. How do you dispatch to your own thread and then wait for yourself? You can't, you will be deadlocked waiting for yourself to run the runnable, which you can't do, because you are busy waiting for yourself.


BTW, the fix isn't to change it to the invokeLater() method. The fix is to check to see if you are running the code in the event dispatching thread. And if so, call the run() method directly.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cannot call invokeAndWait, can't invokeLater either
 
Similar Threads
Layout Problems
Stuck on setVisible()
Haw to separate a frame to diferent fraims
GUI Design issue
JButtons fail to change