java.lang.IllegalStateException: not in swing thread! - invokeAndWait not workin

Hey,



i try to create a new jmedesktop, in an the actionlistener of a button.

I get the following exception: java.lang.IllegalStateException: not in swing thread!

I was looking about it in other threads, and i found that i shall use the invokeAndWait method.

so i tried:


   SwingUtilities.invokeAndWait(
         new Runnable() {
         public void run() {
                   MenuState_Main main = new MenuState_Main()
                }
    } );



But then I get the following exception

java.lang.reflect.InvocationTargetException
at java.awt.EventQueue.invokeAndWait(EventQueue.java:997)
at javax.swing.SwingUtilities.invokeAndWait(SwingUtilities.java:1323)
at com.jmex.awt.swingui.JMEDesktop.onButton(Unknown Source)
at com.jmex.awt.swingui.JMEDesktop$ButtonAction.performAction(Unknown Source)
at com.jme.input.ActionTrigger.performAction(Unknown Source)
at com.jme.input.InputHandler.processTriggers(Unknown Source)
at com.jme.input.InputHandler.update(Unknown Source)
at com.jme.input.InputHandler.updateAttachedHandlers(Unknown Source)
at com.jme.input.InputHandler.update(Unknown Source)
at vig.game.menus.baseclasses.BaseMenuState.update(BaseMenuState.java:116)
at vig.game.menus.jmedesktops.MenuState_Welcome.update(MenuState_Welcome.java:230)
at com.jmex.game.state.GameStateNode.update(Unknown Source)
at com.jmex.game.StandardGame.update(Unknown Source)
at com.jmex.game.StandardGame.run(Unknown Source)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.Error: Cannot call invokeAndWait from the event dispatcher thread
at java.awt.EventQueue.invokeAndWait(EventQueue.java:980)
at javax.swing.SwingUtilities.invokeAndWait(SwingUtilities.java:1323)
at vig.game.menus.jmedesktops.MenuState_Welcome$1$2.actionPerformed(MenuState_Welcome.java:147)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6041)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5806)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4413)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4243)
at com.jmex.awt.swingui.JMEDesktop.dispatchEvent(Unknown Source)
at com.jmex.awt.swingui.JMEDesktop.sendAWTMouseEvent(Unknown Source)
at com.jmex.awt.swingui.JMEDesktop.access$1000(Unknown Source)
at com.jmex.awt.swingui.JMEDesktop$6.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:199)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

i'm confused - what is the corret way to do that?

but unfortunately it is not  :frowning: :frowning: :frowning:



first I'm creating all the menu from my main class…  - the swing stuff is than injected into the swing thread by the jmedesktopstate…

so the next time i want to create a new menu, the actionPerformed is executed in the swing thread - is this correct or am i wrong… and there i got the problem, beacause there I'm getting the java.lang.reflect.InvocationTargetException except. and when I'm starting a new thread in the jme thread for creating the gamestate i get "not in swing thread" even when i m using SwingUtilities.invokeAndWait(



I don't get it…



:x


You should check if you are in the swing Event Dispatch Thread before calling invokeAndWait(), then only call it if you are not in the EVT. http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/SwingUtilities.html#isEventDispatchThread()

Seems like the code that you call from invokeAndWait does another invokeAndWait, which is not allowed, since it would cause a deadlock.

thx for your answer…



now i'm trying it form another thread…

there i don't get the InvocationTargetException, but again the not in swingthread exception, even when I'm using  SwingUtilities.invokeAndWait  :? :? :? :? :? :?



i have no idea why

again:



this is my code now


class GameStateLoader extends Thread{
      
      public GameStateLoader(){
         
      }
      public void run(){
         //GameStateManager.getInstance().detachAllChildren();
         try {
            SwingUtilities.invokeAndWait(
                  new Runnable() {
                     public void run() {
                        MenuState_Main main = new MenuState_Main(CoreGame.JMEDesktop_Main,parentGame,coreGame);/                        
                     }
                  }
            );
         } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
         MenuState_Main main = new MenuState_Main(CoreGame.JMEDesktop_Main,parentGame,coreGame);
      }
   }



I'm starting this thread out of the action performed method of another gui..... and  I'm still getting "not in swing thread"

:x

Well you do it both ways in that one… on should go wrong at least right?