Advanced Java Programming T.
E (E&C)
Date:
Experiment No.:01
Aim: Write a program to demonstrate status of key on an Applet window such as KeyPressed,
KeyReleased, KeyUp, KeyDown.
Objective: The objective of this assignment is to learn the concepts of Keyboard event handling using
Applet.
Outcome:
The practical is expected to develop the following skill:
1. Able to implement the events of KeyListerner on an Applet window.
Theory:
An event can be defined as changing the state of an object or behavior by performing actions. Actions
can be a button click, cursor movement, keypress through keyboard or page scrolling, etc.
The java.awt.event package can be used to provide various event classes.
Classification of Events:
1. Foreground Events
Foreground events are the events that require user interaction to generate, i.e., foreground events are
generated due to interaction by the user on components in Graphic User Interface (GUI). Interactions
are nothing but clicking on a button, scrolling the scroll bar, cursor moments, etc.
2. Background Events
Events that don’t require interactions of users to generate are known as background events. Examples of
these events are operating system failures/interrupts, hardware or software failure, timer expires,
operation completion, etc.
Event Handling:
It is a mechanism to control the events and to decide what should happen if an event occurs. This
mechanism has the code which is known as event handler that is executed when an event occurs. To
handle the events, Java follows the Delegation Event model.
Department of Electronics and Computer Engineering
Advanced Java Programming T.E (E&C)
Delegation Event model:
1. Source: The source is an object on which event occurs. Source is responsible for providing
information of the occurred event to its handler. Java provides classes for source object. There are
various sources like buttons, checkboxes, list, menu-item, choice, scrollbar, text components,
windows, etc., to generate events.
2. Listeners: Listeners are used for handling the events generated from the source. Each of these
listeners represents interfaces that are responsible for handling events. From java implementation
point of view the listener is also an object. Listener waits until it receives an event. Once the event
is received, the listener processes the event and then returns.
To perform Event Handling, we need to register the source with the listener.
Registering the Source with Listener
Different Classes provide different registration methods.
Syntax:
public void addTypeListener(TypeListener el)
Here, Type is the name of the event, and el is a reference to the event listener.
Example 1: For KeyEvent we use addKeyListener() to register.
Example 2: that For ActionEvent we use addActionListener() to register.
A source must also provide a method that allows a listener to unregister an interest in a specific type of
event. The general form of such a method is this:
public void removeTypeListener(TypeListener el)
Here, Type is the name of the event, and el is a reference to the event listener. For example, to remove a
keyboard listener, you would call removeKeyListener( ).
The methods that add or remove listeners are provided by the source that generates events. For example,
the Component class provides methods to add and remove keyboard and mouse event listeners.
A KeyEvent is generated when keyboard input occurs. Whenever a user presses any key on the
Keyboard, different events are fired. There are three types of key events,
which are identified by these integer constants: KEY_PRESSED, KEY_RELEASED, and
Department of Electronics and Computer Engineering
Advanced Java Programming T.E (E&C)
KEY_TYPED. The first two events are generated when any key is pressed or released. The
last event occurs only when a character is generated. Remember, not all keypresses result in characters.
For example, pressing SHIFT does not generate a character.
The KeyEvent class defines several methods, but the most commonly used ones are getKeyChar( ),
which returns the character that was entered. Their general forms are shown here:
char getKeyChar( )
Event Class Listener Interface Methods Description
keyTyped ( ) Called just after the user types a Unicode
character into the listened-to component.
keyPressed ( ) Called just after the user presses a key while
KeyEvent KeyListener the listened-to component has the focus.
keyReleased ( ) Called just after the user releases a key while
the listened-to component has the focus.
Applet:
An applet is a program written in the Java programming language that can be included in an HTML
page, much in the same way an image is included in a page. When you use a Java technology-enabled
browser to view a page that contains an applet, the applet's code is transferred to your system and
executed by the browser's Java Virtual Machine (JVM). The applet will be executed by a Java-enabled
web browser when it encounters the APPLET tag within the HTML file. To view and test an applet
more conveniently, simply include a comment at the head of your Java source code file that
contains the APPLET tag. Here is an example of such a comment:
/*
<applet code="MyApplet" width=200 height=60>
</applet>
*/
This comment contains an APPLET tag that will run an applet called MyApplet in a
window that is 200 pixels wide and 60 pixels high.
Applet Life Cycle:
Department of Electronics and Computer Engineering
Advanced Java Programming T.E (E&C)
init ()
Figure 1: Applet state transition diagram
Initialization state:
Applet enters the initialization state when it is first loaded. This is achieved by calling the init() method
of Applet class.
Create objects needed by the applet.
Set up initial values.
Load images or fonts.
Set up colors.
Running state:
Applet enters the running state when the system calls the start() method of Applet class. This occurs
automatically after the applet is initialized. Starting can also occur if the applet is already in “stopped”
(idle) state.
Idle or Stopped State:
An applet becomes idle when it is stopped from running. Stopping occurs automatically when we leave
the page containing the currently running applet. We can calling the stop () method explicitly.
Dead State:
Department of Electronics and Computer Engineering
Advanced Java Programming T.E (E&C)
An applet is said to be dead when it is removed from memory. This occurs automatically by invoking
the destroy() method when we quit the browser.
Display State:
Applet moves to the display state whenever it has to perform some output operation on the screen. This
happens immediately after the applet enters into the running state.
Simple Applet Display Methods:
To output a string to an applet, use drawString( ), which is a member of the Graphics class. Typically,
it is called from within either update( ) or paint( ). It has the following general form:
void drawString(String message, int x, int y)
Here, message is the string to be output beginning at x,y. In a Java window, the upper-left corner is
location 0,0.
In addition to displaying information in its window, an applet can also output a message
to the status window of the browser or applet viewer on which it is running. To do so, call
showStatus( ) with the string that you want displayed. The status window is a good place
to give the user feedback about what is occurring in the applet, suggest options, or possibly
report some types of errors.
Sample output is shown here:
Department of Electronics and Computer Engineering
Advanced Java Programming T.E (E&C)
Conclusion:
U P R T Sign
Questions:
1. What Are The Applets Life Cycle Methods? Explain Them?
2. Which classes can an applet extend?
3. How Do Applets Differ From Applications?
4. What is an event and what are the models available for event handling?
5. How Events are handled? Explain with one example.
Department of Electronics and Computer Engineering