Applet Life Cycle in Java:
• In Java, an applet is a special type of program embedded in the web
page to generate dynamic content. Applet is a class in Java.
• The applet life cycle can be defined as the process of how the object is
created, started, stopped, and destroyed during the entire execution of
its application.
• It basically has five core methods namely init(), start(), stop(), paint()
and destroy().These methods are invoked by the browser to execute.
• Along with the browser, the applet also works on the client side, thus
having less processing time.
➢There are five methods of an applet life cycle, and they are:
1) init():
• The init() method is the first method to run that initializes the applet. It can be
invoked only once at the time of initialization.
• The web browser creates the initialized objects, i.e., the web browser (after
checking the security settings) runs the init() method within the applet.
2) start():
• The start() method contains the actual code of the applet and starts the applet.
• It is invoked immediately after the init() method is invoked. Every time the
browser is loaded or refreshed, the start() method is invoked.
• It is also invoked whenever the applet is maximized, restored, or moving from
one tab to another in the browser. It is in an inactive state until the init() method
is invoked.
3) stop():
• The stop() method stops the execution of the applet.
• The stop () method is invoked whenever the applet is stopped, minimized, or
moving from one tab to another in the browser, the stop() method is invoked.
• When we go back to that page, the start() method is invoked again.
4) destroy():
• The destroy() method destroys the applet after its work is done.
• It is invoked when the applet window is closed or when the tab containing the
webpage is closed.
• It removes the applet object from memory and is executed only once. We cannot
start the applet once it is destroyed.
5) paint():
• The paint() method belongs to the Graphics class in Java.
• It is used to draw shapes like circle, square, trapezium, etc., in the applet.
• It is executed after the start() method and when the browser or applet windows
are resized.
Applet Life Cycle Working:
• The Java plug-in software is responsible for managing the life cycle of
an applet.
• An applet is a Java application executed in any web browser and
works on the client-side. It doesn't have the main() method because it
runs in the browser. It is thus created to be placed on an HTML page.
• The init(), start(), stop() and destroy() methods belongs to
the [Link] class.
• The paint() method belongs to the [Link] class.
• In Java, if we want to make a class an Applet class, we need to extend
the Applet
• Whenever we create an applet, we are creating the instance of the
existing Applet class. And thus, we can use all the methods of that
class.
• Flow of Applet Life Cycle:
These methods are invoked by the browser automatically. There is no need to call
them explicitly.
Syntax of entire Applet Life Cycle in Java:
class TestAppletLifeCycle extends Applet {
public void init() {
// initialized objects
}
public void start() {
// code to start the applet
}
public void paint(Graphics graphics) {
// draw the shapes
}
public void stop() {
// code to stop the applet
}
public void destroy() {
// code to destroy the applet
}
}
//[Link](int x,int y,int height, int width);
//This method will draw an oval at specified x and y position with given height and width.
//[Link](int x,int y,int height, int width);
//This method will fill an oval at specified x and y position with given height and width.
AWT vs Swing
Creating Frame Window by Instantiating Frame class:
import [Link].*;
public class Testawt
{
Testawt()
{
Frame fm=new Frame(); //Creating a frame
Label lb = new Label("welcome to java graphics");
//Creating a label [Link](lb); //adding label to
the frame
[Link](300, 300); //setting frame size
[Link](true); //set frame visibilty true
}
public static void main(String args[])
{
Testawt ta = new Testawt();
}
}
Creating Frame window by extending Frame class:
package testawt;
import [Link].*;
import [Link].*;
public class Testawt extends Frame
{
public Testawt()
{
Button btn=new Button("Hello World");
add(btn); //adding a new Button.
setSize(400, 500); //setting size.
setTitle("StudyTonight"); //setting title.
setLayout(new FlowLayout()); //set default layout for
frame. setVisible(true); //set frame visibilty true.
}
public static void main (String[] args)
{
Testawt ta = new Testawt(); //creating a frame.
}
}
AWT Button Class Declaration:
import [Link].*;
public class ButtonDemo1
{
public static void main(String[] args)
{
Frame f1=new Frame("studytonight ==> Button Demo");
Button b1=new Button("Press Here");
[Link](80,200,80,50);
[Link](b1);
[Link](500,500);
[Link](null);
[Link](true);
}
}
Label Declaration:
import [Link].*;
class LabelDemo1
{
public static void main(String args[])
{
Frame l_Frame= new Frame("studytonight ==> Label Demo");
Label lab1,lab2;
lab1=new Label("Welcome to [Link]");
[Link](50,50,200,30);
lab2=new Label("This Tutorial is of Java");
[Link](50,100,200,30);
l_Frame.add(lab1);
l_Frame.add(lab2);
l_Frame.setSize(500,500);
l_Frame.setLayout(null);
l_Frame.setVisible(true);
}
}
TextField Declaration:
import [Link].*;
class TextFieldDemo1{
public static void main(String args[]){
Frame TextF_f= new Frame("studytonight ==>TextField");
TextField text1,text2;
text1=new TextField("Welcome to studytonight");
[Link](60,100, 230,40);
text2=new TextField("This tutorial is of Java");
[Link](60,150, 230,40);
TextF_f.add(text1);
TextF_f.add(text2);
TextF_f.setSize(500,500);
TextF_f.setLayout(null);
TextF_f.setVisible(true);
}
}
TextArea Declaration:
import [Link].*;
public class TextAreaDemo1
{
TextAreaDemo1()
{
Frame textArea_f= new Frame();
TextArea area=new TextArea("Welcome to [Link]");
[Link](30,40, 200,200);
textArea_f.add(area);
textArea_f.setSize(300,300);
textArea_f.setLayout(null);
textArea_f.setVisible(true);
}
public static void main(String args[])
{
new TextAreaDemo1();
}
}
Checkbox Syntax:
import [Link].*;
public class CheckboxDemo1
{
CheckboxDemo1(){
Frame checkB_f= new Frame("studytonight ==>Checkbox Example");
Checkbox ckbox1 = new Checkbox("Yes", true);
[Link](100,100, 60,60);
Checkbox ckbox2 = new Checkbox("No");
[Link](100,150, 60,60);
checkB_f.add(ckbox1);
checkB_f.add(ckbox2);
checkB_f.setSize(400,400);
checkB_f.setLayout(null);
checkB_f.setVisible(true);
}
public static void main(String args[])
{
new CheckboxDemo1();
}
}
CheckboxGroup Declaration:
import [Link].*;
public class CheckboxGroupDemo
{
CheckboxGroupDemo(){
Frame ck_groupf= new Frame("studytonight ==>CheckboxGroup");
CheckboxGroupobj = new CheckboxGroup();
Checkbox ckBox1 = new Checkbox("Yes", obj, true);
[Link](100,100, 50,50);
Checkbox ckBox2 = new Checkbox("No", obj, false);
[Link](100,150, 50,50);
ck_groupf.add(ckBox1);
ck_groupf.add(ckBox2);
ck_groupf.setSize(400,400);
ck_groupf.setLayout(null);
ck_groupf.setVisible(true);
}
public static void main(String args[])
{
new CheckboxGroupDemo();
}
}
Choice Declaration:
import [Link].*;
public class ChoiceDemo
{
ChoiceDemo()
{
Frame choice_f= new Frame();
Choice obj=new Choice();
[Link](80,80, 100,100);
[Link]("Red");
[Link]("Blue");
[Link]("Black");
[Link]("Pink");
[Link]("White");
[Link]("Green");
choice_f.add(obj);
choice_f.setSize(400,400);
choice_f.setLayout(null);
choice_f.setVisible(true);
}
public static void main(String args[])
{ new ChoiceDemo(); }
}
List Declaration:
import [Link].*;
public class ListDemo
{
ListDemo()
{
Frame list_f= new Frame();
List obj=new List(6);
[Link](80,80, 100,100);
[Link]("Red");
[Link]("Blue");
[Link]("Black");
[Link]("Pink");
[Link]("White");
[Link]("Green");
list_f.add(obj);
list_f.setSize(400,400);
list_f.setLayout(null);
list_f.setVisible(true);
}
public static void main(String args[])
{ new ListDemo(); }
}
Swing Jbutton:
import [Link].*;
import [Link].*;
import [Link].*;
public class testswing extends JFrame
{
testswing()
{
JButton bt1 = new JButton("Yes"); //Creating a Yes Button.
JButton bt2 = new JButton("No"); //Creating a No Button.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) //setting close operation.
setLayout(new FlowLayout()); //setting layout using FlowLayout object
setSize(400, 400); //setting size of Jframe
add(bt1); //adding Yes button to frame.
add(bt2); //adding No button to frame.
setVisible(true);
}
public static void main(String[] args)
{
new testswing();
}
}
JTextField:
import [Link].*;
import [Link].*;
import [Link].*;
public class MyTextField extends JFrame
{
public MyTextField()
{
JTextField jtf = new JTextField(20); //creating JTextField.
add(jtf); //adding JTextField to frame.
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
}
public static void main(String[] args)
{
new MyTextField();
}
}
JCheckBox:
import [Link].*;
import [Link].*;
import [Link].*;
public class Test extends JFrame
{
public Test()
{
JCheckBox jcb = new JCheckBox("yes"); //creating JCheckBox.
add(jcb); //adding JCheckBox to frame.
jcb = new JCheckBox("no"); //creating JCheckBox.
add(jcb); //adding JCheckBox to frame.
jcb = new JCheckBox("maybe"); //creating JCheckBox.
add(jcb); //adding JCheckBox to frame.
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
}
public static void main(String[] args)
{
new Test();
}
JRadioButton:
import [Link].*;
import [Link].*;
import [Link].*;
public class Test extends JFrame
{ public Test() {
JRadioButton jcb = new JRadioButton("A"); //creating JRadioButton.
add(jcb); //adding JRadioButton to frame.
jcb = new JRadioButton("B"); //creating JRadioButton.
add(jcb); //adding JRadioButton to frame.
jcb = new JRadioButton("C"); //creating JRadioButton.
add(jcb); //adding JRadioButton to frame.
jcb = new JRadioButton("none");
add(jcb);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
}
public static void main(String[] args){
new Test(); }
}
JComboBox:
import [Link].*;
import [Link].*;
import [Link].*;
public class Test extends JFrame
{
String name[] = {"Abhi","Adam","Alex","Ashkay"}; //list of name.
public Test()
{
JComboBox jc = new JComboBox(name); //initialzing combo box with list of name.
add(jc); //adding JComboBox to frame.
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
}
public static void main(String[] args)
{
new Test();
}
}
Jlabel:
import [Link].*;
class SLabelDemo1
{
public static void main(String args[])
{
JFrame label_f= new JFrame("studytonight ==> Label Demo");
JLabel label_l1,label_l2;
label_l1=new JLabel("Welcome to [Link]");
label_l1.setBounds(50,50, 200,30);
label_l2=new JLabel("How are You?");
label_l2.setBounds(50,100, 200,30);
label_f.add(label_l1);
label_f.add(label_l2);
label_f.setSize(300,300);
label_f.setLayout(null);
label_f.setVisible(true);
}
}
JTextArea:
import [Link].*;
public class STextAreaDemo1
{
STextAreaDemo1()
{
JFrame textArea_f= new JFrame();
JTextArea textArea_area=new JTextArea("Welcome to [Link] ");
textArea_area.setBounds(10,30, 200,200);
textArea_f.add(textArea_area);
textArea_f.setSize(400,400);
textArea_f.setLayout(null);
textArea_f.setVisible(true);
}
public static void main(String args[])
{
new STextAreaDemo1();
}
}
JPasswordField:
import [Link].*;
public class SPasswordFieldDemo1
{
public static void main(String[] args)
{
JFrame passWord_f=new JFrame("studytonight ==> Password Field Demo");
JPasswordField passWord_value = new JPasswordField();
JLabel passWord_l1=new JLabel("Password ");
passWord_l1.setBounds(20,100, 100,30);
passWord_value.setBounds(100,100,100,30);
passWord_f.add(passWord_value);
passWord_f.add(passWord_l1);
passWord_f.setSize(300,300);
passWord_f.setLayout(null);
passWord_f.setVisible(true);
}
}
Jtable:
import [Link].*;
public class STableDemo1
{
JFrame table_f;
STableDemo1(){
table_f=new JFrame();
String table_data[][]={ {"1001","Cherry"}, {"1002","Candy"}, {"1003","Coco"}};
String table_column[]={"SID","SNAME"};
JTable table_jt=new JTable(table_data,table_column);
table_jt.setBounds(30,40,200,300);
JScrollPane table_sp=new JScrollPane(table_jt);
table_f.add(table_sp);
table_f.setSize(300,400);
table_f.setVisible(true);
}
public static void main(String[] args)
{
new STableDemo1(); }
}
Jlist:
import [Link].*;
public class SListDemo
{
SListDemo()
{ JFrame list_f= new JFrame();
DefaultListModel<String> list_l1 = new DefaultListModel<>();
list_l1.addElement("Red");
list_l1.addElement("Pink");
list_l1.addElement("Blue");
list_l1.addElement("Black");
JList<String> list1 = new JList<>(list_l1);
[Link](100,100, 75,75);
list_f.add(list1);
list_f.setSize(400,400);
list_f.setLayout(null);
list_f.setVisible(true);
}
public static void main(String args[]) {
new SListDemo(); }
}