Professional Documents
Culture Documents
Java - Applets 1 PDF
Java - Applets 1 PDF
Java - Applets 1 PDF
Applet Programming
An applet is a Java program that runs in a Web browser. An applet can be a fully functional
Java application because it has the entire Java API at its disposal. There are certain
differences between Applet and Java Standalone Application that are described below:
2. A main() method is not invoked on an applet, and an applet class will not define
main().
4. When a user views an HTML page that contains an applet, the code for the applet
is downloaded to the user's machine.
5. A JVM is required to view an applet. The JVM can be either a plug-in of the Web
browser or a separate runtime environment.
6. The JVM on the user's machine creates an instance of the applet class and invokes
various methods during the applet's lifetime.
7. Applets have strict security rules that are enforced by the Web browser. The
security of an applet is often referred to as sandbox security, comparing the applet
to a child playing in a sandbox with various rules that must be followed.
1. init() Method: This method is intended for whatever initialization is needed for your
applet. It is called after the param tags inside the applet tag have been processed.
2. start() method: This method is automatically called after the browser calls the init()
method. It is also called whenever the user returns to the page containing the applet
after having gone off to other pages.
3. stop() method: This method is automatically called when the user moves off the
page on which the applet sits. It can, therefore, be called repeatedly in the same
applet.
4. destroy() method: This method is only called when the browser shuts down
normally. Because applets are meant to live on an HTML page, you should not
normally leave resources behind after a user leaves the page that contains the applet.
5. paint() method: Invoked immediately after the start() method, and also any time the
applet needs to repaint itself in the browser. The paint() method is actually inherited
from the java.awt.
//Code of MyFirstApplet.java
Output
Method – 1: Open the HTML File in Java Enabled Web Browser
//Code of PARAM_Test.html
Output
//Code of user_input.java
//Code of user_input.html
Output
//Code of Applet_image.java
//Code of Applet_image.html
Output
An applet can play an audio file represented by the AudioClip interface in the java.applet
package. The AudioClip interface has three methods, including:
1. public void play(): Plays the audio clip one time, from the beginning.
2. public void loop(): Causes the audio clip to replay continually.
3. public void stop(): Stops playing the audio clip.
//Code of Applet_audio.java
Output
2. TextField
The textField component allows the user to edit single line of text. When the user types a
key in the text field the event is sent to the TextField. The key event may be key pressed,
Key released or key typed.
Example
Output
3. Button
Button is a control component that has a label and generates an event when pressed. When
a button is pressed and released, AWT sends an instance of ActionEvent to the button, by
calling processEvent on the button.
Example
Output
1
JAVA NOTES BY ACHIN JAIN
0
JAVA NOTES – ACHIN JAIN - ASSISTANT PROFESSOR, CSE(NIEC)
4. CheckBox
Checkbox control is used to turn an option on (true) or off (false). There is label for each
checkbox representing what the checkbox does. The state of a checkbox can be changed by
clicking on it. In AWT there is no Radio button class as in Swings, so to create Radio button
CheckboxGroup class is used. The CheckboxGroup class is used to group the set of
checkbox.
Example
Output
1
JAVA NOTES BY ACHIN JAIN
1
JAVA NOTES – ACHIN JAIN - ASSISTANT PROFESSOR, CSE(NIEC)
5. List
The List represents a list of text items. The list can be configured to that user can choose
either one item or multiple items.
Example
Output
1
JAVA NOTES BY ACHIN JAIN
2
JAVA NOTES – ACHIN JAIN - ASSISTANT PROFESSOR, CSE(NIEC)
6. Choice
Choice control is used to show pop up menu of choices. Selected choice is shown on the
top of the menu. It is same as List the only difference is with the appearance of the control
on window.
Example
Output
1
JAVA NOTES BY ACHIN JAIN
3
JAVA NOTES – ACHIN JAIN - ASSISTANT PROFESSOR, CSE(NIEC)
7. TextArea
The TextArea control in AWT provide us multiline editor area. The user can type here as
much as he wants. When the text in the text area become larger than the viewable area the
scroll bar is automatically appears which help us to scroll the text up & down and right &
left.
Example
Output
1
JAVA NOTES BY ACHIN JAIN
4
JAVA NOTES – ACHIN JAIN - ASSISTANT PROFESSOR, CSE(NIEC)
8. ScrollBar
Scrollbar control represents a scroll bar component in order to enable user to select from
range of values.
1. static int HORIZONTAL --A constant that indicates a horizontal scroll bar.
2. static int VERTICAL --A constant that indicates a vertical scroll bar.
Output
1
JAVA NOTES BY ACHIN JAIN
5
JAVA NOTES – ACHIN JAIN - ASSISTANT PROFESSOR, CSE(NIEC)
9. Menus in JAVA
Example
1
JAVA NOTES BY ACHIN JAIN
6
JAVA NOTES – ACHIN JAIN - ASSISTANT PROFESSOR, CSE(NIEC)
Event Handling
Event is the change in the state of the object or source. Events are generated as result of
user interaction with the graphical user interface components. For example, clicking on a
button, moving the mouse, entering a character through keyboard, selecting an item from
list, scrolling the page are the activities that causes an event to happen.
Event Handling is the mechanism that controls the event and decides what should happen
if an event occurs. This mechanism have the code which is known as event handler that is
executed when an event occurs. Java Uses the Delegation Event Model to handle the events.
This model defines the standard mechanism to generate and handle the events. The
Delegation Event Model has the following key participants namely:
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 provide as with
classes for source object.
2. Listener - It is also known as event handler. Listener is responsible for generating
response to an event. 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 process the event then returns.
The benefit of this approach is that the user interface logic is completely separated from
the logic that generates the event. The user interface element is able to delegate the
processing of an event to the separate piece of code. In this model, Listener needs to be
registered with the source object so that the listener can receive the event notification. This
is an efficient way of handling the event because the event notifications are sent only to
those listener that want to receive them.
1
JAVA NOTES BY ACHIN JAIN
7
JAVA NOTES – ACHIN JAIN - ASSISTANT PROFESSOR, CSE(NIEC)
1. ActionListener Example
This interface is used to handle the events caused by sources like Buttons, Menu Items,
Enter Keys and Double Click of Mouse. When you implements ActionListener Interface
following methods needs to be override.
Example
1
JAVA NOTES BY ACHIN JAIN
8
JAVA NOTES – ACHIN JAIN - ASSISTANT PROFESSOR, CSE(NIEC)
Output
Used for Radio Button, List, Choice and Check Box AWT Controls. The method that needs
to be override is as follows:
Example
Output
1
JAVA NOTES BY ACHIN JAIN
9
JAVA NOTES – ACHIN JAIN - ASSISTANT PROFESSOR, CSE(NIEC)
3. AdjustmentListener Example
Used to handle events generated from Scrollbar source. This interface has only method
which is: public void adjustmentValueChanged(AdjustmentEvent e)
Example
Output
4. KeyListener Example
This interface is used to handle the events generated from keys of the keyboard. There are
three methods present in the KeyListener Interface and all methods needs to be override.
2
JAVA NOTES BY ACHIN JAIN
0
JAVA NOTES – ACHIN JAIN - ASSISTANT PROFESSOR, CSE(NIEC)
Example
Output
2
JAVA NOTES BY ACHIN JAIN
1
JAVA NOTES – ACHIN JAIN - ASSISTANT PROFESSOR, CSE(NIEC)
5. MouseMotionListener Example
As the name suggests this interface is used to handle events generated from Mouse Source.
Methods are as follows:
Example
2
JAVA NOTES BY ACHIN JAIN
2
JAVA NOTES – ACHIN JAIN - ASSISTANT PROFESSOR, CSE(NIEC)
Output
7. MouseListener Example
Adapter Classes
Adapter classes are used to simplify the process of event handling in Java. As we know that
when we implement any interface all the methods defined in that interface needs to be
override in the class, which is not desirable in the case of Event Handling. Adapter classes
are useful as they provide empty implementation of all methods in an event listener
interface. In this you can define a new class to act as event listener by extending one of the
2
JAVA NOTES BY ACHIN JAIN
3
JAVA NOTES – ACHIN JAIN - ASSISTANT PROFESSOR, CSE(NIEC)
adapter classes and implementing only those methods that you want to use in your
program.
2
JAVA NOTES BY ACHIN JAIN
4
JAVA NOTES – ACHIN JAIN - ASSISTANT PROFESSOR, CSE(NIEC)
Example
Layout Manager
BorderLayout arranges the components to fit in the five regions: east, west, north, south
and center. Each region is can contain only one component and each component in each
2
JAVA NOTES BY ACHIN JAIN
5
JAVA NOTES – ACHIN JAIN - ASSISTANT PROFESSOR, CSE(NIEC)
region is identified by the corresponding constant NORTH, SOUTH, EAST, WEST, and
CENTER.
Example
Suppose you have created 5 buttons from b1….b5. To add each button specific to each
direction see the following code.
Output
2
JAVA NOTES BY ACHIN JAIN
6
JAVA NOTES – ACHIN JAIN - ASSISTANT PROFESSOR, CSE(NIEC)
2. Card Layout
The class CardLayout arranges each component in the container as a card. Only one card is
visible at a time, and the container acts as a stack of cards.
Example
2
JAVA NOTES BY ACHIN JAIN
7
JAVA NOTES – ACHIN JAIN - ASSISTANT PROFESSOR, CSE(NIEC)
3. Flow Layout
The class FlowLayout components in a left-to-right flow. It is the default manager for
panels and applets.
Example
Output
4. Grid Layout
As the name indicates the container is divided into a grid of cells dictated
by rows and columns. Each cell accommodates one component. Like with FlowLayout, in
GridLayout also, the position need not be mentioned. But remember, in BorderLayout,
position is to be mentioned. As the rows and columns are fixed, components addition goes
one after another automatically.
GridLayout of 3 rows and 4 columns (12 cells) is shown below in the figure.
2
JAVA NOTES BY ACHIN JAIN
8
JAVA NOTES – ACHIN JAIN - ASSISTANT PROFESSOR, CSE(NIEC)
Example
Output
2
JAVA NOTES BY ACHIN JAIN
9
JAVA NOTES – ACHIN JAIN - ASSISTANT PROFESSOR, CSE(NIEC)
Swings
Swing is a part of JFC(Java Foundation Classes) that is used to create GUI application. It is
built on the top of AWT and entirely written in java.
3
JAVA NOTES BY ACHIN JAIN
0
JAVA NOTES – ACHIN JAIN - ASSISTANT PROFESSOR, CSE(NIEC)
3
JAVA NOTES BY ACHIN JAIN
1