I modified the sample code for Activator posted on the Oracle/Sun AWT tutorial page here
The modification is as follows
f.add(new MyCanvas(f.getGraphicsConfiguration()),
BorderLayout.CENTER);
The paint method in MyCanvas is overridden as follows
MyCustomRunnable mcr = new MyCustomRunnable();
Thread th = new Thread(mcr);
th.start();
while(Thread.currentThread().isAlive()){
mcr.getData();
//do UI stuff
Thread.yield();
}
Similarly MyCustomRunnable has a corresponding loop in run()
public void run(){
while(Thread.currentThread().isAlive){
//do Stuff
Thread.yield();
}
}
The Runnable, and Canvas paint (both) run a loop. With this bit of code running, System Menu close on the UI window are not invoked. Why?
The short answer is that
paint()is called on the event thread, which is also the thread that handles all UI events, and you are taking over that thread and putting it into an infinite loop.When you are doing this in the paint method…
… the “current thread” you are working on is the same thread you entered that method on, which is the “event dispatch thread”. I would guess that what you really wanted was to run a background thread that periodically repaints the view. You could do this in the constructor of your AWT component:
Note that I also created a boolean variable
runBackgroundThreadwhich would be a volatile field in the component class. Setting it false would stop the loading thread. In contrast,Thread.currentThread().isAlive()will always be true – the currently running thread must by definition be alive.