Taking screenshots from event dispatch thread

I have a program runing JOGL with multiple JFrames with GLCanvases on them. I want to take a screenshot from one of those panels in response to a GUI event. The problem is that the event dispatch thread is not apparently the right thread to be calling from - I get this exception:

Exception in thread “AWT-EventQueue-0” javax.media.opengl.GLException: No OpenGL context current on this thread

I suppose this should have been obvious, but at this point, I’m not sure how to proceed - does anyone have a clever way of getting around this? My first thought is to set a state flag that tells the screenshot to be taken on the next repaint, but the model I’m using for this application would require some significant changes to do that… any other ideas?

  1. Take the screenshot from the EDT with SwingUtitlities.invokeLater()

  2. Store that image in some reference that is accessible from your rendering-thread.

  3. Take the pixels from the texture and upload them to a texture.

If you accept the delay, use SwingUtilities.invokeAndWait() for convienience, but it will block your render-thread for quite a few milliseconds.

My recommendation would be for you to add a little logic to your display() method to see whether you are taking a snapshot and do something a little different, and then in your event handler simply set that bit and call display() on the appropriate GLCanvas, which should be a synchronous call from the point of view of the event handler. This is the easiest and most correct way of doing what you are after. You can play games with manually making the context current but if the above technique works for you it will be the most portable and robust way of doing it.

Thanks to both of you (although I think Ken’s method is indeed the simplest - I’ll probably use that…). But Riven, I’m not sure I understand your method. If I take the screenshot with SwingUtitlities.invokeLater, won’t that’s still be on the EDT thread (i.e. it won’t be able to find the current GLContext)?