Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot call invokeAndWait, can't invokeLater either

 
Sam Smith
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic