adding components to a JApplet using threads

I'm in the process of converting a game to run in a JApplet. The problem i'm running into at the moment is that when the game starts I spawn a thread to do all my processing in and want to change the currently displayed JPanel from that thread.

Here is a simplified version of what i'm trying to do. If I add the JButton in the init() thread everything works fine, but if I try to do it in the thread that I create nothing happens. From the thread I create I can do things like change the text of the button, but can't seem to add or remove components.

import javax.swing.JApplet;

import javax.swing.JButton;

/**

*

* @author alex

*/

publicclass Mainextends JAppletimplements Runnable{

/** Creates a new instance of Main */

public Main(){

}

publicvoid start(){

Thread t =new Thread(this);

t.start();

}

publicvoid run(){

this.add(new JButton("TEST"));

}

}

If instead of creating my own thread I use SwingUtilities.invokeAndWait this works, but I don't want to run my program in that thread. I've tried using invokeAndWait from the thread I create but that doesn't seem to work.

Anyone have any ideas ?

[2009 byte] By [skorulisa] at [2007-11-27 10:34:28]
# 1

hi

your simplified version have got no error on my machine. may be you can call

validate() and other method related to this, to check whether it is added or not, and you can also check with the getComponent(0)

regards

Aniruddha

Aniruddha-Herea at 2007-7-28 18:29:05 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks for help, adding validate() in seemed to fix my problems, in my frames based version I call pack() and validate() seems to do something similar.

Go to www.moojoogames.com/dev/jooballs.html if you want to see the result.

skorulisa at 2007-7-28 18:29:05 > top of Java-index,Desktop,Core GUI APIs...