Applet
Applet
is a small program which executes on browser or Java supported browser. There
are two types of applets: Local and Remote.
1.
Local
Applets are those which execute on local machine. There is no need of internet
for local applet.
2.
Remote
applets are those which exist on another or remote machine. We can download and
execute them on local machine.
Following
is the life cycle of an applet:
1. Initialization
(used to initialize methods, variables, etc.)
2. Start
(execution starts from this method)
3. Paint
(used to display output)
4. Stop
(used to stop execution)
5. Destroy
(used to de-allocate memory)
Format:
public void init()
{}
public void start()
{}
public void paint()
{}
public void stop()
{}
public void destroy()
{}
Compilation and
Execution
Compilation
à
javac filename.java
Execution
à
appletviewer filename.java
Note:
The class-name and file-name are same in case of applets.
e.g.
(basic)
Note:
·
Browser only
understands markup language. That’s why, we write the tags in comments section.
·
No need of main() in
applets.
e.g.
(using some methods)
Color codes:
Red
= 255,0,0
Green
= 0,255,0
Blue
= 0,0,255
White
= 0,0,0
Black
= 255,255,255
e.g.
(experimenting with colors)
Note:
Class
Graphics and Color lie in awt.
e.g.
(drawing shapes)
Controls
in awt:
·
Label – static
·
Button
·
Text Field
·
Checkbox
·
Radio-button
·
List-box
·
Combo-box
·
Scrollbar
·
Text Area
Steps
to use these:
1. Initialize
the class
Format: class_name
object_name = new class_name();
2. Add
into the component class
Format: add(object_name)
Label is
static control and has 3 constructors:
1. Label();
2. Label(String
str);
3. Label(String
str, Allignment);
Button
is a class used for event purpose. It has 2 constructors.
e.g.
(adding Buttons)
Text Field
is a class used for input purpose. It has 3 constructors:
1. Default
constructor
TextField();
2. TextField(10);
3. TextField(String
str);
e.g.
(including Label, text, button)
Checkbox
is a class that provides true and false value. It has 3 constructors:
1. Checkbox();
2. Checkbox(String
str);
3. Checkbox(String
str, Boolean);
Radio
button
Format:
CheckboxGroup cbg = new ChackboxGroup();
Checkbox ch = new Checkbox(String str,
cbg, boolean);
List
is a group of items through which we can chose one or more than one items. It
has 3 constructors:
1. Default: List();
2. Parametrized: List(int num);
3. List(int
num, Boolean);
Combo-box
– This is similar to List. This allows
only one choice while list may allow more than one option.
Format:
Choice ch = new Choice();
To add:
ch.add();
ch.addItem();
Text
Area is a combination of rows and columns. It
has 3 constructors:
1. TextArea();
2. TextArea(int
row, int column);
3. TextArea(String
str);
Event Delegation Model
Event is
a change in state of an object. Change necessarily requires a source. Events are performed on static
objects.
Requirements:
1. Event
class
2. Sources
3. Listener
Required
Package for event àjava.awt.event.*
Required
Interface à ActionListener (used with button)
3
necessary steps:
1. Initialize
the object
2. Add
into the component
3. Register
the objects
Format: object_name.addInterfaceName(CurrentObject);
e.g. b1.addActionListener(this);
The
ActionEvent class provides the method getActionCommand(). This returns 2 kinds
of values – String.
e.g.
(Registration of object)
e.g.
(performing action)
Ques. WAP
to display:
1. First
number label and textbox
2. Second
number label and textbox
3. Result
label and textbox
4. Buttons
– add, subtract, multiply, divide
(Basic
calculator program)
Class
Interface Event Class Method
Button ActionListener ActionEvent actionPerformed(ActionEvent ae)
TextField ” ” ”
List ” ” ”
Checkbox ItemListener ItemEvent itemStateChanged(ItemEvent)
Radio ” ” ”
Choice ” ” ”
TextArea ActionListener ActionEvent actionPerformed(ActionEvent ae)
Scrollbar AdjustmentListener AdjustmentEvent adjustementValueChanged()
Swings
Swings
are a part of Advanced Java.
Package required: import java.swing.*;
Default layout of awt was “Flow Layout”. In swings, we need to set
layout.
Swings are light weight components which
provide extendibility to awt components. Swings is better than awt in look and
feel. Swings also add further new components. e.g. JLabel, JTable, JScrollPane,
JTree.
JLabel
constructors:
1. JLabel();
2. JLabel(str,
str);
3. JLabel(str,
image);
To add image: ImageIcon ic = new ImageIcon(“Image_path”);
JButton
constructors:
1. JButton();
2. JButton(String
str)
3. JButton(String
str, image)
Demo
ImageIcon ic = new
ImageIcon(“Image_path”);
JLabel l1 = new JLabel(“Name”);
JButton j = new JButton();
JButton j1 = new JButton(“Name”);
JButton j2 = new JButton(“Name”, ic);
cp.add(l1);
cp.add(j);
cp.add(j1);
cp.add(j2);
j1.addActionListener(this);
Checkbox
constructors:
1. JCheckbox();
2. JCheckbox(str);
3. JCheckbox(str,
boolean);
4. JCheckbox(str,
image);
Method
for Checkbox: isSelected();