Java Unit 4
Java Unit 4
Java Unit 4
An applet is a Java program that can be embedded into a web page. It runs inside
the web browser and works at client side. An applet is embedded in an HTML
page using the APPLET or OBJECT tag and hosted on a web server.
Applets are used to make the website more dynamic and entertaining.
Important points :
1. All applets are sub-classes (either directly or indirectly)
of java.applet.Applet class.
2. Applets are not stand-alone programs. Instead, they run within either a web
browser or an applet viewer. JDK provides a standard applet viewer tool
called applet viewer.
3. In general, execution of an applet does not begin at main() method.
4. Output of an applet window is not performed by System.out.println(). Rather it
is handled with various AWT methods, such as drawString().
Life cycle of an applet :
It is important to understand the order in which the various methods shown in the
above image are called. When an applet begins, the following methods are called,
in this sequence:
1. init( )
2. start( )
3. paint( )
When an applet is terminated, the following sequence of method calls takes
place:
1. stop( )
2. destroy( )
Let’s look more closely at these methods.
1. init( ) : The init( ) method is the first method to be called. This is where you
should initialize variables. This method is called only once during the run time of
your applet.
2. start( ) : The start( ) method is called after init( ). It is also called to restart an
applet after it has been stopped. Note that init( ) is called once i.e. when the first
time an applet is loaded whereas start( ) is called each time an applet’s HTML
document is displayed onscreen. So, if a user leaves a web page and comes back,
the applet resumes execution at start( ).
3. paint( ) : The paint( ) method is called each time an AWT-based applet’s
output must be redrawn. This situation can occur for several reasons. For
example, the window in which the applet is running may be overwritten by
another window and then uncovered. Or the applet window may be minimized
and then restored.
paint( ) is also called when the applet begins execution. Whatever the cause,
whenever the applet must redraw its output, paint( ) is called.
The paint( ) method has one parameter of type Graphics. This parameter will
contain the graphics context, which describes the graphics environment in which
the applet is running. This context is used whenever output to the applet is
required.
INTRODUCTION TO AWT:-
java.applet.Applet class
For creating any applet java.applet.Applet class must be inherited. It provides 4 life
cycle methods of applet.
1. public void init(): is used to initialized the Applet. It is invoked only once.
2. public void start(): is invoked after the init() method or browser is
maximized. It is used to start the Applet.
3. public void stop(): is used to stop the Applet. It is invoked when Applet is
stop or browser is minimized.
4. public void destroy(): is used to destroy the Applet. It is invoked only once
AWT CONTROLS:-
• UI elements : Thes are the core visual elements the user eventually sees and
interacts with. GWT provides a huge list of widely used and common
elements varying from basic to complex which we will cover in this tutorial.
• Layouts: They define how UI elements should be organized on the screen
and provide a final look and feel to the GUI (Graphical User Interface). This
part will be covered in Layout chapter.
• Behavior: These are events which occur when the user interacts with UI
elements. This part will be covered in Event Handling chapter.
Component
1 A Component is an abstract super class for GUI controls and it represents an
object with graphical representation.
AWT UI Elements:
Following is the list of commonly used controls while designed GUI using AWT.
Sr.
Control & Description
No.
1 Label
A Label object is a component for placing text in a container.
2 Button
This class creates a labeled button.
Check Box
3 A check box is a graphical component that can be in either an on (true)
or off (false) state.
5 List
The List component presents the user with a scrolling list of text items.
Text Field
6 A TextField object is a text component that allows for the editing of a single line
of text.
Text Area
7 A TextArea object is a text component that allows for the editing of a multiple
lines of text.
8 Choice
A Choice control is used to show pop up menu of choices. Selected choice is
shown on the top of the menu.
Canvas
9 A Canvas control represents a rectangular area where application can draw
something or can receive inputs created by user.
Image
10 An Image control is superclass for all image classes representing graphical
images.
Scroll Bar
11 A Scrollbar control represents a scroll bar component in order to enable user to
select from range of values.
Dialog
12 A Dialog control represents a top-level window with a title and a border used to
take some form of input from the user.
File Dialog
13 A FileDialog control represents a dialog window from which the user can select a
file.
Kickstart Y
BorderLayout (LayoutManagers)
Java LayoutManagers
1. java.awt.BorderLayout
2. java.awt.FlowLayout
3. java.awt.GridLayout
4. java.awt.CardLayout
5. java.awt.GridBagLayout
6. javax.swing.BoxLayout
7. javax.swing.GroupLayout
8. javax.swing.ScrollPaneLayout
9. javax.swing.SpringLayout etc.
Java BorderLayout
The BorderLayout is used to arrange the components in five regions: north, south,
east, west, and center. Each region (area) may contain one component only. It is
the default layout of a frame or window. The BorderLayout provides five constants
for each region:
ActionEvent ActionListener
MouseEvent MouseListener and MouseMotionListener
MouseWheelEvent MouseWheelListener
KeyEvent KeyListener
ItemEvent ItemListener
TextEvent TextListener
AdjustmentEvent AdjustmentListener
WindowEvent WindowListener
ComponentEvent ComponentListener
ContainerEvent ContainerListener
FocusEvent FocusListener
Registration Methods
For registering the component with the Listener, many classes provide the
registration methods. For example:
o Button
o public void addActionListener(ActionListener a){}
o MenuItem
o public void addActionListener(ActionListener a){}
o TextField
o public void addActionListener(ActionListener a){}
o public void addTextListener(TextListener a){}
o TextArea
o public void addTextListener(TextListener a){}
o Checkbox
o public void addItemListener(ItemListener a){}
o Choice
o public void addItemListener(ItemListener a){}
o List
o public void addActionListener(ActionListener a){}
o public void addItemListener(ItemListener a){}
We can put the event handling code into one of the following places:
1. Within class
2. Other class
3. Anonymous class
4. An Interface in Java programming language is defined as an abstract type
used to specify the behavior of a class. An interface in Java is a blueprint of
a behavior. A Java interface contains static constants and abstract methods.
5. What are Interfaces in Java?
6. The interface in Java is a mechanism to achieve abstraction. There can be
only abstract methods in the Java interface, not the method body. It is used
to achieve abstraction and multiple inheritances in Java using Interface. In
other words, you can say that interfaces can have abstract methods and
variables. It cannot have a method body. Java Interface also represents the
IS-A relationship.
7. When we decide on a type of entity by its behavior and not via attribute we
should define it as an interface.