How to correctly enter Swing Thread with Gamestates?

Title is self-explanatory! :stuck_out_tongue:



I'm using a Gamestate for my UI. The UI is made with Swing. Im writing a chat UI and want to add chatmessages to the main chat txtarea. To give you an idea what my code looks like:



Note: It probably doesnt run since I removed a few excessive things.


public class ChatGamestate extends GameState
{
   
    private Node rootNode, chatNode, guiNode;
    private String name;
   
    final JEditorPane mainChatPane = new JEditorPane();
   
    public ChatGamestate(name)
    {
        this.name = name;
        rootNode = new Node(name + ": RootNode");


        guiNode = new Node( "gui" );
        guiNode.setRenderQueueMode( Renderer.QUEUE_ORTHO );
       
        final JMEDesktopState desktopState = new JMEDesktopState(passedGame.getDisplay().getWidth(),passedGame.getDisplay().getHeight());
        desktopState.setName("chat");
       
        chatGui.attachChild(desktopState);
        desktopState.setActive(true);
       
       
       
        GameTaskQueueManager.getManager().update(new Callable<Object>()
        {
            public Object call() throws Exception
            {
   // I create the Swing components here and give them their location, actionListener's and size.
            }
        });
       
        rootNode.attachChild(guiNode);
       
 
    }
   
    public void displayMSG(String msg)
    {
        //This method should display the chatmessage
        //It gets called from a different Thread

            String oldChat = mainChatPane.getText();
            String newChat = oldChat+msg;
            mainChatPane.setText(newChat);
           
            //Im using this line because I want the chatPane to scroll to the bottom automatically when a message is received. However
            //because the Swing Thread keeps running the length isnt always correct.
            mainChatPane.setCaretPosition(mainChatPane.getText().length());
       
            }
   
    public void update(float tpf)
    {
    }
   
    public void render(float tpf)
    {
    }
   
    public void cleanup()
    {
    }
   
}



Thanks! :)

Im still wondering if someone could give me a pointer. Even though I advanced a bit im still thinking im not using the right approach. Right now im still using the approach described in my opening post and i've added methods like:



    public void addPlayerToList(String name)
    {
              listModel.addElement(name);
    }



who get called from a different thread. But since I recall the propper way to update a Swing-GUI is calling the Swing-thread im a bit confused. This method doesnt work well by the way, sometimes it adds elements but they won't show up and the other time they will show up.

Thanks in advance




A hint in what direction i have to search would also be appreciated! :smiley:

have you tried with SwingUtilities.invokeLater

Haven't tried it yet, but wasn't this more an issue arround the GameTaskQueue and OpenGL Thread rather than the Swing thread? Meaning that I have to find the answer at the GameTaskQueue?

theprism said:

have you tried with SwingUtilities.invokeLater


Worked like a charm, thanks a million!