I experiment with my applet using Eclipse and its Applet Viewer. The Applet Viewer appears on the top of Eclipse and during Applet execution I click on Eclipse icon to maximize it from task bar. Then Applet Viewer loses the focus and Applet.stop() gets called.
When I minimize Eclipse, Applet Viewer goes to front again, gains focus and Applet.start() gets called. This ends up in a complete mess.
Is it normal behavior for a browser to call Applet.stop once user changes to another Tab or minimizes the browser Can I disable that, I want stop never been called.
Maybe I am missing something in threads.
My code is something like this:
public class AppletApp extends JApplet {
public void init() {
super.init();
System.out.println("AppletApp.init()");
}
public void start() {
System.out.println("AppletApp.start()");
try {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
getContentPane().add(new JLabel("Test Label"));
}
});
} catch (InterruptedException e) {
} catch (InvocationTargetException e) {}
Runnable runnable = new Runnable() {
@Override
public void run() {
//For DJ Browser Component
NativeSwing.initialize();
NativeInterface.open();
//connect to server and start message exchange
Client.init(userInterface);
userInterface.authenticate();
NativeInterface.runEventPump();
}
};
Thread t = new Thread(runnable);
t.start();
}
public void stop() {
System.out.println("AppletApp.stop()");
}
public void destroy() {
System.out.println("AppletApp.destroy()");
}
}
Yes it is normal. From the javadoc:
When you switch tab, I consider that the containing web page has been replaced hence it is logical that
stop()is invoked.No you can’t, you don’t have control on that. However, you could rely on the
init()and thedestroy()methods instead ofstart()andstop().start()andstop()are meant for resuming/pausing anything that consumes resources which are not necessary if not visible (for example an animation is pointless if not visible).