Applet in Java
Applet in Java is a special Java program that are primarily used in a internet programming.
Applet Life Cycle is very important to know in Java Applet Programming.
As we have already seen how to run program from command prompt, but there were no GUI.
To provide Graphical user interface(GUI) Java Applet is used.
Applet program runs on a web browser at client side. Applet program are used to make the web site more dynamic.
1 Life Cycle of Applet Stages
When the applet program is loaded is passes through several stages:
1 2 3 4 | Born or initialization Running Idle or stopped Dead or terminate |
A Applet Born State
Also known as initialization state.
It is the first state of applet lifecycle.
Applet enters in a born/ initialization state when it is first loaded.
Applet moves in a born state when init()
method of Applet class executes.
Applet initialization occurs once in a applet lifecycle.
Inside the init()
method we initialize all the variables.
Syntax of init() method in Java Applet
1 2 | public void init(){ } |
B Applet Running State
Applet enters in a running state from born state when a start() method of Applet class executes.
Running sates call automatically when the initialization phase completes.
Applet also comes in a running state from idle state when idle applet again
Running sates call automatically when the initialization phase completes.
Applet also comes in a running state from idle state when idle applet again get processor.
Syntax of start()
1 2 | public void start(){ } |
C Applet Idle State
A running appletenters in a idle sate from running state when execution of start complete.
In a another words when we leave the page of running applet then automatically it enters in a idle sate from running state.
Applet moves in idle state when stop() method executes and it moves idle to running state when again start() calls.
Syntax of stop()
1 2 | public void stop(){ } |
D Applet Dead State
An applet is moved to dead state when we quit the web browser. When applet moves in a dead state
The
Syntax of destroy()
1 2 | public void destroy(){ } |
E Applet Display State
The Display state executes immediately after when applet enters in a running state.
In this
1 2 | public void paint(Graphics g){ } |
2 Applet Life Cycle Program in Java Example
Lets see one program to demonstrate the life cycle of applet.
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 | import java.applet.Applet; import java.awt.Graphics; /* <applet code="AppletLifeCycle.class" width="500" height="500"> </applet> */ public class AppletLifeCycle extends Applet { String s; public void init() { s = "Welcome to Java Applet"; System.out.println("Inside init method"); } public void start() { System.out.println("Inside start method"); } public void paint(Graphics g) { System.out.println("Inside paint method"); g.drawString(s, 100, 100); } public void stop() { System.out.println("Inside stop method"); } public void destroy() { System.out.println("Inside destroy method"); } } |
Life cycle methods are called and System.out.println() statement is written to check the execution.
On execution you will get following result.
Compile and Run the program
>javac AppletLifeCycle.java
>appletviewer AppletLifeCycle.java

Output At Console
1 2 3 4 5 | Inside init method Inside start method Inside paint method Inside stop method Inside destroy method |
On running Applet It will print first 3 line from output.
On closing applet it will call stop and destroy method.
init()
method can be used to initialize various values and objects that are going to use in program.
Similarly to release resource we can use destroy()
.
Example: Write a program to read file and show in java applet.
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 40 41 42 43 44 45 46 47 48 49 | import java.applet.Applet; import java.awt.Graphics; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; /* <applet code="AppletLifeCycle.class" width="800" height="800"> </applet> */ public class AppletLifeCycle extends Applet { File file; Scanner scanner; public void init() { sb = new StringBuffer(); file = new File("D:\\test.txt"); System.out.println("Inside init method: created StringBuffer and File Object"); } public void start() { System.out.println("Inside start method"); } public void paint(Graphics g) { System.out.println("Inside paint method"); try { int i = 0; scanner = new Scanner(file); while (scanner.hasNextLine()) { g.drawString(scanner.nextLine(), 100, 100 + i); i += 30; } } catch (FileNotFoundException ex) { System.out.println("Exception " + ex); } System.out.println("Reading file and showing at applet"); } public void stop() { System.out.println("Inside stop method"); } public void destroy() { scanner.close(); System.out.println("Inside destroy method: closed scanner object"); } } |
Output
1 2 3 4 5 6 | Inside init method: created StringBuffer and File Object Inside start method Inside paint method Reading file and showing at applet Inside stop method Inside destroy method: closed scanner object |
