UNIT-V Applets
UNIT-V Applets
Introduction:
UNIT-V 2
Life Cycle:
• init
– To initialize the applet each time it's loaded (or reloaded).
• start
– To start the applet's execution, such as when the applet's loaded
or when the user revisits a page that contains the applet.
• stop
– To stop the applet's execution, such as when the user leaves the
applet's page or quits the browser.
• destroy
– To perform a final cleanup in preparation for unloading.
UNIT-V 3
Life Cycle:
start( )
init( ) destroy( )
stop( )
UNIT-V 4
paint( ):
paint( )
repaint( )
update( )
UNIT-V 5
Hierarchy:
Component
Container
Window Panel
Frame Applet
UNIT-V 6
Sample Applet Program:
import java.applet.*;
import java.awt.*;
/*
<applet code="Sample1" width=500 height=500>
</applet>
*/
public class Sample1 extends Applet {
String msg;
public void init( ) {
msg="Hello";
}
public void start( ) {
msg+="Start";
}
public void paint(Graphics g) {
msg+="paint";
g.drawString(msg,50,50);
}
} UNIT-V 7
HTML program to run an Applet on Webbrowser:
<head>
<title> The Life Cycle of an Applet </title>
</head>
<body>
<applet code=“Sample1" width="500“ height=“500">
</applet>
</body>
</html>
UNIT-V 8
Alternate way to run an Applet:
C:\...\>appletviewer Sample1.java
UNIT-V 9
The <APPLET> Tag:
< APPLET
[CODEBASE = codebaseURL]
CODE = appletFile
[ALT = alternateText]
[NAME = appletInstanceName]
WIDTH = pixels
HEIGHT = pixels
[ALIGN = alignment]
[VSPACE = pixels]
[HSPACE = pixels]
>
[< PARAM NAME = appletParameter1 VALUE = value >]
[< PARAM NAME = appletParameter2 VALUE = value >]
...
[alternateHTML]
</APPLET>
UNIT-V 10
Methods of Applet class:
Applet class is in java.applet package.
To set colors for a component, Component class have defined two methods:
UNIT-V 12
Color class:
Constants defined in Color class are: Constructors defined in Color class are:
Color.black
Color(int red, int green, int blue)
Color.blue
Color(int rgbValue)
Color.cyan
Color(float red, float green, float blue)
Color.gray
Color.darkGray Methods defined in Color class are:
To set a different font for a component, Component class have defined one
method:
UNIT-V 15
Font class:
The general form of Font class constructor is:
Font(String fontName, int fontStyle, int pointStyle)
fontName can be Dialog, DialogInput, Sans Serif, Serif, Monospaced & Symbol.
fontStyle can be any of the constants: Font.PLAIN, Font.BOLD, Font.ITALIC.
fontStyle can be combine style in the form Font.PLAIN|Font.ITALIC.
• Labels
• Buttons
• Check boxes
• Choice lists
• Lists
• Scroll bars
• Text editing
UNIT-V 17
Adding Controls:
Steps to add a control to a container (Applet or Frame):
UNIT-V 18
Removing Controls:
UNIT-V 19
Label:
Labels are passive controls that do not support any interaction with the user.
UNIT-V 20
Button:
A button (push button) is a component that contains a label and that generates
an event when it is pressed.
Methods:
void setLabel(String str)
String getLabel( )
UNIT-V 21
Checkbox:
A check box is a control that is used to turn an option on or off (check mark).
Each time a check box is selected or deselected, an item event is generated.
Checkbox class Constructors:
Checkbox( )
Checkbox(String str)
Checkbox(String str, boolean on)
Checkbox(String str, boolean on, CheckboxGroup cbg)
Checkbox(String str, CheckboxGroup cbg, boolean on)
Checkbox methods: CheckboxGroup methods:
void setLabel(String str) Checkbox getSelectedCheckbox( )
String getLabel( ) void setSelectedCheckbox(Checkbox
cb)
void setState(boolean on)
boolean getState( ) UNIT-V 22
Choice:
The Choice is used to create a pop-up list of items.
Each time a choice is selected, an item event is generated.
Choice class contains only the default constructor.
Choice methods:
void addItem(String name)
void add(String name)
String getSelectedItem( )
int getSelectedIndex( )
int getItemCount( )
void select(int index)
void select(String name)
String getItem(int index)
UNIT-V 23
List:
The List provides a compact, multiple-choice, scrolling selection list.
A list shows a number of choices in the visible widow. It also allow multiple
selections.
List constructors:
List( )
List(int numRows)
List(int numRows, boolean multipleSelect)
UNIT-V 25
Scrollbar:
Scrollbars are used to select continuous values between a specified minimum
and maximum.
Scrollbar constructors:
Style can be,
Scrollbar( )
Scrollbar.VERTICAL
Scrollbar(int style)
Scrollbar.HORIZONTAL
Scrollbar(int style, int initialValue, int thumbSize, int min, int max)
Scrollbar methods:
void setValues(int initialValue, int thumbSize, int min, int max)
int getValue( )
void setValue(int newValue)
int getMinimum( )
int getMaximum( )
void setUnitIncrement(int newIncr)
UNIT-V 26
void setBlockIncrement(int newIncr)
TextField:
TextField is a single line text entry area. TexField is subclass of TextComponent.
TextField constructors:
TextField( )
TextField(int numChars)
TextField(String str)
TextField(String str, int numChars)
TextField methods:
int getSelectionStart( )
String getText( )
int getSelectionEnd( )
void setText(String str)
void setEchoChar(char ch)
String getSelectedText( )
boolean echoCharIsSet( )
void select(int startIndex, int endIndex)
char getEchoChar( )
boolean isEditable( )
UNIT-V 27
void setEditable(boolean canEdit)
TextArea:
TextArea is a multiline editor. TextArea is subclass of TextComponent.
TextArea constructors:
TextArea( )
TextArea(int numLines, int numChars)
TextArea(String str)
TextArea(String str, int numLines, int numChars)
TextArea(String str, int numLines, int numChars, int scrBars)
scrBars can be
SCROLLBARS_BOTH
SCROLLBARS_NONE
SCROLLBARS_HORIZONTAL_ONLY
SCROLLBARS_VERTICAL_ONLY
UNIT-V 28
TextArea:
TextArea methods:
String getText( )
void setText(String str)
String getSelectedText( )
void select(int startIndex, int endIndex)
boolean isEditable( )
void setEditable(boolean canEdit)
int getSelectionStart( )
int getSelectionEnd( )
void append(String str)
void insert(String str, int index)
void replaceRange(String str, int startIndex, ine endIndex)
UNIT-V 29
Menu Bars and Menus:
Menus in Java are implemented by the use of the following classes:
MenuBar
Menu
MenuItem
CheckboxMenuItem
UNIT-V 30
Menu:
Constructors:
Menu( )
Menu(String optionName)
Menu(String optionName, boolean removable)
MenuItem:
Constructors:
MenuItem( )
MenuItem(String itemName)
MenuItem(String itemName, MenuShortcut keyAccel)
UNIT-V 31
CheckboxMenuItem:
Constructors:
CheckboxMenuItem( )
CheckboxMenuItem(String itemName)
CheckboxMenuItem(String itemName, boolean on)
MenuShortcut:
Constructors:
MenuShortcut(int key)
UNIT-V 32
Layout Managers:
A layout manager is used by a container to position each of the components
with in it.
Each container has a layout manager associated with it.
A layout manager is an instance of any class that implements the interface
LayoutManager.
The layout manager is set by:
void setLayout(LayoutManager layoutObj)
Java defines the following layout manager classes:
1. FlowLayout
2. BorderLayout
3. GridLayout
4. CardLayout
5. GridbagLayout
UNIT-V 33
FlowLayout:
FlowLayout is the default layout manager.
With this components are laid out from the upper-left corner, left to right and
top to bottom line by line.
Constructors:
FlowLayout( )
FlowLayout(int how)
FlowLayout(int how, int horz, int vert)
Valid how values are
FlowLayout.LEFT
FlowLayout.CENTER
FlowLayout.RIGHT
UNIT-V 34
BorderLayout:
With BorderLayout, a conatiner has four narrow fixed-width components at
the edges and one large are in the center. The four sides are referred to as
north, south, east and west. The middle area is called the center.
Constructors:
BorderLayout( )
BorderLayout(int horz, int vert)
To add components use the following add method:
void add(Component compObj, Object region)
The region can be
BorderLayout.CENTER
BorderLayout.EAST
BorderLayout.WEST
BorderLayout.NORTH
UNIT-V 35
BorderLayout.SOUTH
GridLayout:
GridLayout lays out components in a two-dimensional grid.
Constructors:
GridLayout( )
GridLayout(int numRows, int numColumns)
GridLayout(int numRows, int numColumns, int horz, int vert)
UNIT-V 36
CardLayout:
Constructors:
CardLayout( )
CardLayout(int horz, int vert)
To add cards to a panel:
void add(Component panelObj, Object name)
Methods:
void first(Conatiner deck)
void last(Conatiner deck)
void next(Conatiner deck)
void previous(Conatiner deck)
void show(Conatiner deck, String cardName)
UNIT-V 37
GridbagLayout
UNIT-V 38