javaswingappletinvokelaterinvokeandwait

Why invokeAndWait() is preferred for applets and not for standalone applications?


The java docs says:

In an applet, the GUI-creation task must be launched from the init() method using invokeAndWait(); otherwise, init() may return before the GUI is created, which may cause problems for a web browser launching an applet.

What is wrong if init() returns before GUI is created? What kind of problems could be caused for the browser?

The docs further says that:

In any other kind of program, scheduling the GUI-creation task is usually the last thing the initial thread does, so it doesn't matter whether it uses invokeLater() or invokeAndWait().

How does GUI-creation task being the last thing done changes anything? Also, it says about the usual practice, if GUI-creation is not the last task being done by the initial thread, would still it not matter if invokeAndWait() is used or invokeLater()?

I think i understand what they are trying to say but I still want to be sure, hence, me posting the question. Thanx in advance!!


Solution

  • What is wrong if init() returns before GUI is created? What kind of problems could be caused for the browser?

    My primary concern would be that the start() method is called prior to the component creation being concluded. If components are referenced in the start method, this would lead to a NullPointerException.

    ..couldn't the same thing happen with stand-alone programs?

    Desktop applications don't have a start() method. The method is part of the applet life-cycle, but not that of applications.