Applet Life Cycle in Java with Example

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:

Java Applet Life Cycle
Java Applet Life Cycle

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

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 get processor.

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()              

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()               

D Applet Dead State

  An applet is moved to dead state when we quit the web browser.  When applet moves in a dead state it removed from memory. 

The Applet will be terminate when destroy () method calls. In a applet lifecycle, applet terminates/destroys ones.

Syntax of destroy()        

E Applet Display State

The Display state executes immediately after when applet enters in a running state.

In this state we display the information on the output screen using paint() method.

2 Applet Life Cycle Program in Java Example

Lets see one program to demonstrate the life cycle of applet.

Compile and Run

>javac AppletLifeCycle.java
>appletviewer AppletLifeCycle.java

Applet Life Cycle in Java Example
Fig: Java Applet Life Cycle Example

Output At Console

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.

Output

Fig: Reading File in showing in an applet

Read More

  1. JComboBox in Java Swing
  2. JTable in Java Swing
  3. JTable Pagination in Java JDBC
  4. Registration Form in Java Swing
  5. Login form in Java Swing
  6. Simple Calculator in Java Applet

Simple Calculator in Java Applet

Java Applet provides GUI way to create program and its interface, Here we are creating Simple Calculator in Java Applet with GUI and its processing.

As we have already learned about applet and applet life cycle.

Here we have discussed two simple calculator program using applet.

  1. Simple applet- a beginner program
  2. Simple Calculator – with calculator functionality

1 Simple calculator program in java using applet

Here we use a simple Applet with two Input box to get Input and result is shown on next input box.

To perform Addition, Subtraction, Multiplication and Division.

Four buttons bill be provided.

Added Action Listener to each button.

Based on button click  public void actionPerformed(ActionEvent e) is get called and and based on event source appropriate if statement is executed.

Java applet program for calculator is as below

Below is the output of the calculator program in java

Simple Calculator in Java Applet
Java Applet Simple Calculator Example

Above Example is create to learn Basic Functionality.

2 Java applet program for calculator

This GUI looks like a real calculator. Here all values are input through button click.

Here We have created buttons for 0-9 input.

Operator buttons +, -, *, /, % and =.

Also provided button for AC, BSp, . (dot), and RSET (Reset).

Functions of these button are

AC– Clear the Input Text Field.

BSp– Backspace used to remove a character from input string.

dot(.)– Dot is used for decimal number.

RSET-Reset both text box input and result box.

All Above Features are included in Example.

Additional Feature of this calculator is that it can accept multiple input with different operators.

After adding all numbers it shows the result in top text box.

Simple Calculator in Java Applet
Simple Java Applet Calculator

Simple Calculator Code

  • Created Layout with two text box and numbers and special buttons like AC, BSp.
  • Add add buttons to  actionListener b[i].addActionListener(this);
  • Create actionPerformed to get actionCommand()
  • Define work for =, AC, BSp and RESET
  • On = arithmetic calculation should be done that is handled by calculate()
  • On calculate() use  ScriptEngineManager for calculation

Calculator.java

In Java ScriptEngineManager is used for expression calculation.

That makes our calculation part easy.

Read More

  1. JLabel in Java Swing
  2. JComboBox in Java Swing
  3. JTable in Java Swing
  4. JTable Pagination in Java JDBC
  5. Registration Form in Java Swing
  6. Login form in Java Swing

Basics of Applet in Java

The Java Applet is a special Java program that are primarily used in a internet programming.

Applet program runs on a web browser at client side.

Applet program are used to make the web site more dynamic.

Applet program embedded in a HTML page and hosted on a web server.

Applet Skeleton/ Structure

Applet skeleton is a structure/ syntax of applet program. 

Any applet program created by us must be sub class of Applet class. Applet program may contain five methods: