Java Programming U6
Java Programming U6
Java Programming U6
Unit-VI
Applets and AWT
Properties of Applets:
• Applet programs doesn’t contain main() method
• An applet cannot run any executable program in the client
system
• An applet cannot communicate with any other server other
than the server from which it was downloaded
Java Programming RAGHU EDUCATIONAL INSTITUTIONS 3
Applets
Properties of Applets:
• An applet program cannot read a file or write into a file that
belongs to the client computer
• All life cycle methods need not require for every applet
program
• Applet programs are executed in a web browser or in
AppletViewer
Web pages can contain two types of applets which are named
after the location at which they are stored.
1. Local Applet
2. Remote Applet
Java Programming RAGHU EDUCATIONAL INSTITUTIONS 4
Applets
Local Applet:
A local applet is the one that is stored on our own
computer system. When the Web-page has to find a local
applet, it doesn't need to retrieve information from the
Internet.
A local applet is specified by a path name and a file
name as shown below in which the codebase attribute
specifies a path name, whereas the code attribute specifies the
name of the byte-code file that contains the applet's code.
<applet codebase="http://www.raghuenggcollege.com"
code="MyApp.class" width=200 height=200> </applet>
Java Programming RAGHU EDUCATIONAL INSTITUTIONS 6
Applet life cycle
Running state: When the user visited the page in which the
applet loaded, immediately applet starts it’s running by
invoking a method called start() method. This method is
invoked every time user revisits the applet page. The form of
start() method is:
public void start(){ … statements … }
Java Programming RAGHU EDUCATIONAL INSTITUTIONS 8
Applet life cycle
Stopped state: When the user minimizes the page in
which applet is running then applet is in stopped (paused)
state by invoking a method called stop() method. This method
is invoked every time the user minimizes the applet page. The
form of stop() method is:
public void stop(){ …statements …}
ActionEvent class:
An ActionEvent is generated when a button is pressed
or a list-item is double clicked or menu-item is selected.
Constant:
ACTION_PERFORMED
Method:
String getActionCommand() – It is used to know the
command(string) invoked by the user.
Java Programming RAGHU EDUCATIONAL INSTITUTIONS 22
Event Handling: sources of event
AdjustmentEvent class:
An AdjustmentEvent generated when a scroll bar is
manipulated. There are six adjustment events.
Constants:
BLOCK_DECREMENT – When user clicks inside the scroll
bar to decrease its value
Constants:
COMPONENT_HIDDEN – When component hidden
COMPONENT_SHOWN – When component shown
COMPONENT_MOVED – When component moved
COMPONENT_RESIZED – When component resized
Method:
Component getComponent() – It returns the component that
generated an event
Java Programming RAGHU EDUCATIONAL INSTITUTIONS 25
Event Handling: sources of event
ContainerEvent class:
A ContainerEvent is generated when a component is
added or removed from a container.
Constants:
COMPONENT_ADDED
COMPONENT_REMOVED
FocusEvent class:
A FocusEvent is generated when a component gained
or lost input focus.
Constants:
FOCUS_GAINED
FOCUS_LOST
Java Programming RAGHU EDUCATIONAL INSTITUTIONS 26
Event Handling: sources of event
InputEvent class:
The abstract InputEvent is a sub class of
ComponentEvent and is a super class for KeyClass and
MouseEvent class.
ItemEvent class:
An ItemEvent is generated when a Checkbox or a list-
item or when a checkable menu-item is selected or de-
selected.
Constants:
SELECTED – When user select an item
DESELECTED – When user deselect an item
Java Programming RAGHU EDUCATIONAL INSTITUTIONS 27
Event Handling: sources of event
ItemEvent class:
Method:
getItem() – Returns the reference of the selected item
getStateChange() – Returns the state change of the item
KeyEvent class: generated when keyboard input occurs.
Constants:
KEY_PRESSED – When any key pressed
KEY_TYPED – When a character key is pressed
KEY_RELEASED – When a pressed or typed key is released
Methods:
int getKeyCode() –To know the code of the key pressed or
typed
char getKeyChar() – To know the character of the key typed.
Java Programming RAGHU EDUCATIONAL INSTITUTIONS 28
Event Handling: sources of event
MouseEvent class:
There are several events generated by the mouse. They
are listed below:
Constants:
MOUSE_CLICKED – When clicked the mouse
MOUSE_DRAGGED – Keeps the mouse clicked then move
it
MOUSE_ENTERED – When mouse entered a component
MOUSE_EXITED – When mouse exited a component
MOUSE_MOVED – When mouse is moved
MOUSE_PRESSED – When mouse is pressed
MOUSE_RELEASED – Mouse is released after the pressing
Java Programming RAGHU EDUCATIONAL INSTITUTIONS 29
Event Handling: sources of event
MouseEvent class: Methods:
int getX() – Returns the x-coordinate value of the cursor position
int getY() – Returns the y-coordinate value of the cursor position
Point getPoint() – Returns x&y coordinate values of the cursor
position. After that we can use Point_reference.x and
Point_reference.y to get the coordinates.
int getClickCount() – Returns the number of mouse clicks
MouseWheelEvent class:
This is generated by the mouse wheel.
Constants:
WHEEL_BLOCK_SCROLL – When page-up or page-down
WHEEL_UNIT_SCROLL – When line-up or line-down
occurred
Java Programming RAGHU EDUCATIONAL INSTITUTIONS 30
Event Handling: sources of event
TextEvent class:
These events are generated when the text character of
text fields and text areas are modified or entered.
Constants:
TEXT_VALUE_CHANGED – When the value of text
component is modified
WindowEvent class:
There are ten types of events generated by the window.
ActionListener Interface:
This interface defines the method actionPerformed()
that is invoked when an action event occurs.
ComponentListener Interface:
It defines four methods which are invoked when component
event occurred.
public void componentResized(ComponentEvent ce)
public void componentMoved(ComponentEvent ce)
public void componentShown(ComponentEvent ce)
public void componentHidden(ComponentEvent ce)
Java Proramming RAGHU EDUCATIONAL INSTITUTIONS 35
Event Handling: Event Listeners
ContainerListener Interface:
It defines two methods to track the container events.
public void componentAdded(ContainerEvent ce)
public void componentRemoved(ContainerEvent ce)
FocusListener Interface:
It defines two methods to track the keyboard focus over the
component.
public void focusGained(FocusEvent fe)
public void focusLost(FocusEvent fe)
ItemListener Interface:
It defines one method to track the changes of an item.
public void itemStateChanged(ItemEvent ie)
KeyListener Interface:
It defines three methods to track the key events.
public void keyPressed(KeyEvent ke)
public void keyTyped(KeyEvent ke)
public void keyReleased(KeyEvent ke)
TextListener Interface:
It defines a method to track the text event.
public void textValueChanged(TextEvent te)
WindowFocusListener Interface:
It defines methods to track the window focus events.
public void windowGainedFocus(WindowEvent we)
public void windowLostFocus(WindowEvent we)
Java Proramming RAGHU EDUCATIONAL INSTITUTIONS 39
Event Handling: Event Listeners
WindowListener Interface:
It defines the methods to track all the window events except
focus events of the window.
public void windowActivated(WindowEvent we)
public void windowDeactivated(WindowEvent we)
public void windowOpened(WindowEvent we)
public void windowClosing(WindowEvent we)
public void windowClosed(WindowEvent we)
public void windowIconified(WindowEvent we)
public void windowDeiconified(WindowEvent we)
Types of components:
Button, Canvas, Checkbox, Choice, Label, List,
Scrollbar, TextArea, and TextField.
Java Proramming RAGHU EDUCATIONAL INSTITUTIONS 45
AWT: Components & Containers
Containers:
• Components do not stand alone, but rather are found within
containers. Containers contain and control the layout of
components.
Window:
The Window class creates a top-level window. A top-
level window is not contained within any other object; it sits
directly on the desktop. Generally, we won’t create Window
objects directly. Instead, we will use a subclass of Window
called Frame, described next.
Frame:
Frame encapsulates what is commonly thought of as a
window. It is a subclass of Window and has a title bar, menu
bar, borders, and resizing corners.
Java Proramming RAGHU EDUCATIONAL INSTITUTIONS 48
AWT: Container
Methods:
void addActionListener(ActionListener l) -Adds the
specified action listener to receive action events from this
button.
String getLabel() -Gets the label of this button.
Java Proramming RAGHU EDUCATIONAL INSTITUTIONS 50
AWT: Button Component
Methods:
void setLabel(String label) -Sets the button's label to be the
specified string.
Constructors:
Constructors of Checkbox:
Checkbox( ) –creates checkbox with empty label
Checkbox(String str) –creates checkbox with label
Checkbox(String str, boolean on) - creates checkbox with
label and initially can put a check mark
Checkbox(String str, boolean on, CheckboxGroup cbGroup)–
used to create Radio Button
Java Proramming RAGHU EDUCATIONAL INSTITUTIONS 54
AWT: Checkbox Component
Constructors of Checkbox:
Checkbox(String str, CheckboxGroup cbGroup, boolean on)
- used to create Radio Button and selects the one radio button
initially.
Constructors:
1. TextField( ) used to create default text editor
2. TextField(int numChars) used to create a text filed with
some number of characters length.
3. TextField(String str)
used to create a text field with string in it.
4. TextField(String str, int numChars)
used to create text filed with string in it and also specified
number of characters length.
Java Proramming RAGHU EDUCATIONAL INSTITUTIONS 61
AWT: TextField Component
Methods:
1. String getText( ) -used to get the text from the text field
2. void setText(String str) -used to set the text to the text field
3. String getSelectedText( ) -used to get the selected text
4. void select(int startIndex, int endIndex) -used to select the
characters starting from startindex to the endindex.
5. void setEchoChar(char ch) -used to create passwords.
Actual characters are not shown.
6. char getEchoChar( ) -used to obtain the echo characters.
Program to demonstrate Button, Label and TextField
Methods:
1. void append(String str) - appends at the end of the current
text area
2. void insert(String str, int index) -inserts the string at the
specified index
3. void replaceRange(String str, int startIndex, int endIndex )-
To replace text, call replaceRange( ). It replaces the
characters from startIndex to endIndex–1, with the
replacement text passed in str.
Java Proramming RAGHU EDUCATIONAL INSTITUTIONS 64
AWT: Layouts
Program to demonstrate AWT controls
Constructors:
1. GridLayout():
2. GridLayout(int rows, int columns):
3. GridLayout(int rows, int columns, int hgap, int vgap):
creates a grid layout with the given rows and columns along
with given horizontal and vertical gaps.
Constructors:
1. CardLayout(): creates a card layout with zero horizontal
and vertical gap.
2. CardLayout(int hgap, int vgap): creates a card layout
with the given horizontal and vertical gap.
Scrollbar( )
Scrollbar(int style)
Scrollbar(int style, int initialValue, int thumbSize, int min, int max)
Scrollbar( )
Scrollbar(int style)
Scrollbar(int style, int initialValue, int thumbSize, int min, int max)