Hello friends today I’m going to show you life-cycle of Java Applet. Applet is a Java Program which runs in browser and can also be run and tested using appletviewer command in java. Applet runs in browser that means it is executed at client side. To run applet, client should have Java installed in his/her computer. Applet code is downloaded from web server before it is executed.
Applet Life-cycle Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import java.awt.*; import java.applet.Applet; import javax.swing.JOptionPane; public class AppletLifecycle extends Applet { TextArea messages = new TextArea(8, 30); public AppletLifecycle() { add(messages); messages.append("Constructor executed\n"); } public void init() { messages.append("init method executed\n"); } public void start() { messages.append("start method executed\n"); } public void paint(Graphics g) { messages.append("Paint called\n"); } public void stop() { JOptionPane.showMessageDialog(null, "Stop method executed"); } public void destroy() { JOptionPane.showMessageDialog(null, "Destory method executed"); } } /* <applet code="AppletLifecycle.class" width="300" height="300">Not supported</applet> */ |
To run this applet using appletviewer command type appletviewer AppletLifecycle.java in command prompt.
To run this applet in browser create a html file and copy commented applet tag code in html file, and open that html file in browser.
Output in AppletViewer
After clicking close button of this window we will get below message box
After clicking OK button we will get below message box
Thanks..
You can share if you like it
Comments