When is Applet.stop() called?

本秂侑毒 提交于 2019-12-11 16:51:37

问题


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()");
    }
}

回答1:


Is it normal behavior for a browser to call Applet.stop once user changes to another Tab or minimizes the browser?

Yes it is normal. From the javadoc:

Called by the browser or applet viewer to inform this applet that it should stop its execution. It is called when the Web page that contains this applet has been replaced by another page, and also just before the applet is to be destroyed.

When you switch tab, I consider that the containing web page has been replaced hence it is logical that stop() is invoked.

Can I disable that, I want stop never been called.

No you can't, you don't have control on that. However, you could rely on the init() and the destroy() methods instead of start() and stop(). start() and stop() 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).



来源:https://stackoverflow.com/questions/14137470/when-is-applet-stop-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!