• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Cannot call invokeAndWait, can't invokeLater either

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Paddy spent all of his days in the O'Furniture back yard with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic