In detail, how do the applet life cycle methods init(), start(), paint(), stop(), and destroy() work?

John Zukowski

The life cycle methods of an applet are init(), start(), stop(), and destroy(). While frequently grouped with the methods, the paint() method is not really part of the applet's life cycle. The method is just called by the browser whenever a part of the screen has become invalidated or in response to a programmer-generated repaint request. The update() method falls into the same category as paint().

As far as the other methods go:

  • init() - called once when applet is loaded
  • start() - called every time the page the applet is located on is loaded, after init() or after leaving the page and returning
  • stop() - called every time the page the applet is located on is left
  • destroy() - called when applet is unloaded

If loading a serialized version of an applet from a .ser file (OBJECT attribute in <APPLET> tag instead of CODE attribute), the init() method is not called.

Use the start() / stop() method pair to deal with starting and stopping threads, as when the page isn't active it isn't nice to use up resources.

Don't rely on the destroy() method being called to free a crucial resource, in case the browser crashes.

I had heard that start() / stop() were called for iconifying and deiconifying, but a test with the latest browser versions shows they aren't called.

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact