java.lang.IllegalStateException: Cannot run from EDT

Hi All,



Back to my usual problem(s) of starting / stopping the game. Having started from scratch on integrating the game with swing, I now have 2 JPanels. When switching between them I want to be able to start / stop the 3D game:

[java]if (mainTabbedPane.getSelectedIndex() == 1) {

java.awt.EventQueue.invokeLater(new Runnable() {



public void run() {

terrainViewer.stop();

}

});terrainViewer.stop();

} else {

if (terrainViewer != null) {

java.awt.EventQueue.invokeLater(new Runnable() {



public void run() {

terrainViewer.start();

}

});

}

}[/java]



Stopping it works fine…however upon calling start I get:

java.lang.IllegalStateException: Cannot run from EDT



restart() didn’t work either.



Note: Sorry momoko, normen; I must be driving you guys insane with this by now :wink:

You dont have to run that from the EDT, just dont do it.

Edit: Also, you call the stop method twice in the code you posted, after the invokeLater()!

This is probably an ignorant question, but what is the EDT?

The Event Dispatch Thread of AWT, its the one working off the queue you add the Runnable to with java.awt.EventQueue.invokeLater().

Ah yes. It does the same thing when I don’t add it. The reason that I added it, is because it was doing this odd thing in the first place…

“java.lang.IllegalStateException: Cannot run from EDT”?? Then you are calling that method directly from a button press in AWT and not via the OpenGL queue:

[snippet id=“10”]

This would also explain some other oddities you had.

Thanks. However:



[java]if (mainTabbedPane.getSelectedIndex() == 1) {

terrainViewer.stop();

} else {

if (terrainViewer != null) {

Future fut = terrainViewer.enqueue(new Callable() {



public Object call() throws Exception {

terrainViewer.start();

return null;

}

});

try {

fut.get();

} catch (Exception e) {

e.printStackTrace();

}

}

}[/java]

Does nothing…

Uh, sorry. When the app is not started the queue will not work of course ^^ Use another thread to start the application.

[java]

new Thread(new Runnable(){

public void run(){

terrainViewer.start();

}

}).start();

[/java]

Still causing similar problems than before I’m afraid:

[java]

new Thread(new Runnable() {



public void run() {

// settings = new AppSettings(true);

// settings.setWidth(1151);

// settings.setHeight(768);

//

terrainViewer.setSettings(settings);

terrainViewer.createCanvas(); // create canvas!

canvasContext = (JmeCanvasContext) terrainViewer.getContext();

canvasContext.setSystemListener(terrainViewer);

Dimension dim = new Dimension(1150, 768); //1150

canvasContext.getCanvas().setPreferredSize(dim);

panelMap3D = new JPanel();

panelMap3D.setSize(dim);

panelMap3D.add(canvasContext.getCanvas());

panel3D.add(panelMap3D);



terrainViewer.startCanvas();

}

}).start();

[/java]

^^Gives me a blank, black JPanel.



And:

[java]

new Thread(new Runnable() {



public void run() {

settings = new AppSettings(true);

settings.setWidth(1151);

settings.setHeight(768);



terrainViewer = new TerrainViewer3D(manager);



terrainViewer.setSettings(settings);

terrainViewer.createCanvas(); // create canvas!

canvasContext = (JmeCanvasContext) terrainViewer.getContext();

canvasContext.setSystemListener(terrainViewer);

Dimension dim = new Dimension(1150, 768); //1150

canvasContext.getCanvas().setPreferredSize(dim);

panelMap3D = new JPanel();

panelMap3D.setSize(dim);

panelMap3D.add(canvasContext.getCanvas());

panel3D.add(panelMap3D);



terrainViewer.startCanvas();

}

}).start();

[/java]

…causes the application to crash as with my previous problem (i.e. no error message, no exception)…

Well, maybe the rest of your application is not waiting for the Thread to finish and initializes the application too soon or something?

Nope…doesn’t work no matter how long it waits / timeout…

Does this work for you?

I dont know, I dont setup a test class for every question here, sorry :wink: But I seem to be able to start and stop a canvas from a main application thread, yeah.

If I recall correctly, the canvas should be visible when you call startCanvas() …

I think now I am terminally confused :smiley:

OK, so:

[java]if (mainTabbedPane.getSelectedIndex() == 1) {

terrainViewer.stop();

} else {

if (terrainViewer != null) {

new Thread(new Runnable() {

public void run() {

terrainViewer.createCanvas();

terrainViewer.startCanvas();

}

}).start();[/java]



…doesn’t produce anything. Just a JPanel with the same image on it than when it was stopped.



Would you guys mind giving me the exact steps that allow me to stop and start a canvas within swing? I don’t fully understand this…





Sorry for being a constant pain about this specific issue :slight_smile:

Tried the same with some demo code. Still the same problem :frowning:

Uh, in jMP I dont use any startCanvas() methods or anything… I just use createCanvas(); once and then use getCanvas(); and attach it to the view (on the EDT thread).

Still no luck. So is this how you do it?

[java]



terrainViewer.stop();





new Thread(new Runnable() {



public void run() {

panelMap3D.removeAll();

terrainViewer.createCanvas();

panelMap3D.add(canvasContext.getCanvas());

}

}).start();[/java]



…because it throws a

SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,6,main]

java.lang.NullPointerException

Yes, but from the EDT, so probably w/o any callable stuff for you (I guess the code gets executed on a AWT button press?)

Yes, this is executed on a button press event.



I’m confused. You told me not to call it from the EDT…? “You dont have to run that from the EDT, just dont do it.”



I tried again, but it won’t work.

[java]

java.awt.EventQueue.invokeLater(new Runnable() {



public void run() {

panelMap3D.removeAll();

panelMap3D.add(canvasContext.getCanvas());

terrainViewer.createCanvas();

}

});

[/java]



I get:



SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,6,main]

java.lang.NullPointerException

at com.jme3.renderer.RenderManager.(RenderManager.java:109)



Do you guys have any example code that I could look at? This is all very confusing…

Thanks :slight_smile:

I was talking about startCanvas() as it was throwing an exception, there is a swing canvas test class yeah, its called TestSwingCanvas i think.