Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java Unit 4

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

What is Applet?

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 AWT or Abstract Window Toolkit is an API used for developing


GUI(Graphic User Interfaces) or Window-Based Applications in Java. Java AWT
is part of the Java Foundation Classes (JFC) that provides a way to build
platform-independent graphical applications.
In this AWT tutorial, you will learn the basics of the AWT, including how to
create windows, buttons, labels, and text fields. We will also learn how to add
event listeners to components so that they can respond to user input.
Java AWT Basics
Java AWT (Abstract Window Toolkit) is an API used to create Graphical User
Interface (GUI) or Windows-based Java programs and Java AWT components
are platform-dependent, which means they are shown in accordance with the
operating system’s view. AWT is heavyweight, which means that its components
consume resources from the underlying operating system

Why AWT is Platform Independent?


The Java AWT utilizes the native platform subroutine to create API components
such as TextField, CheckBox, and buttons. This results in a different visual
format for these components on different platforms such as Windows, MAC OS,
and Unix. The reason for this is that each platform has a distinct view of its native
components. AWT directly calls this native subroutine to create the components,
resulting in an AWT application resembling a Windows application on Windows
OS, and a Mac application on the MAC OS. In simpler terms, the AWT
application’s appearance adapts to the platform it is running on.
AWT is platform independent even after the AWT components are platform
dependent because of the points mentioned below:
1. JVM (Java Virtual Machine):
As Java Virtual Machine is platform dependent
2. Abstract APIs:
AWT provides an abstract layer for GUI. Java applications interact with AWT
through Abstract API which are platform independent. Abstract API allows Java
to isolate platform-specific details, making code portable across different
systems.
3. Platform-Independent Libraries:
The Libraries of AWT are written in Java which they are totally platform-
independent. Because of this, it ensures that AWT functionality remains
consistent across different environments.
Java AWT Hierarchy
• Components: AWT provides various components such as buttons, labels, text
fields, checkboxes, etc used for creating GUI elements for Java Applications.
• Containers: AWT provides containers like panels, frames, and dialogues to
organize and group components in the Application.
• Layout Managers: Layout Managers are responsible for arranging data in the
containers sone of the layout managers are BorderLayout, FlowLayout, etc.
• Event Handling: AWT allows the user to handle the events like mouse clicks,
key presses, etc. using event listeners and adapters.
• Graphics and Drawing: It is the feature of AWT that helps to draw shapes,
insert images and write text in the components of a Java Application.

Lifecycle methods for Applet:

The java.applet.Applet class 4 life cycle methods and java.awt.Component class


provides 1 life cycle methods for an applet.

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:-

Every user interface considers the following three main aspects:

• 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.

Every AWT controls inherits properties from Component class.


Sr.
Control & Description
No.

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.

4 Check Box Group


The CheckboxGroup class is used to group the set of checkbox.

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

The LayoutManagers are used to arrange components in a particular manner.


The Java LayoutManagers facilitates us to control the positioning and size of the
components in GUI forms. LayoutManager is an interface that is implemented by
all the classes of layout managers. There are the following classes that represent
the layout managers:

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:

1. public static final int NORTH


2. public static final int SOUTH
3. public static final int EAST
4. public static final int WEST
5. public static final int CENTER

Constructors of BorderLayout class:


o BorderLayout(): creates a border layout but with no gaps between the
components.
o BorderLayout(int hgap, int vgap): creates a border layout with the given
horizontal and vertical gaps between the components.

Event and Listener (Java Event Handling)


Changing the state of an object is known as an event. For example, click on button, dragging mo
java.awt.event package provides many event classes and Listener interfaces for event handling.

Java Event classes and Listener interfaces

Event Classes Listener Interfaces

ActionEvent ActionListener
MouseEvent MouseListener and MouseMotionListener

MouseWheelEvent MouseWheelListener

KeyEvent KeyListener

ItemEvent ItemListener

TextEvent TextListener

AdjustmentEvent AdjustmentListener

WindowEvent WindowListener

ComponentEvent ComponentListener

ContainerEvent ContainerListener

FocusEvent FocusListener

Steps to perform Event Handling

Following steps are required to perform event handling:

1. Register the component with the Listener

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){}

Java Event Handling Code

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.

Syntax for Java Interfaces


interface {

// declare constant fields


// declare methods that abstract
// by default.
}
To declare an interface, use the interface keyword. It is used to provide total
abstraction. That means all the methods in an interface are declared with an
empty body and are public and all fields are public, static, and final by default. A
class that implements an interface must implement all the methods declared in the
interface. To implement the interface use the implements keyword.

Uses of Interfaces in Java


Uses of Interfaces in Java are mentioned below:
• It is used to achieve total abstraction.
• Since java does not support multiple inheritances in the case of class, by using
an interface it can achieve multiple inheritances.
• Any class can extend only 1 class but can any class implement an infinite
number of interface.
• It is also used to achieve loose coupling.
• Interfaces are used to implement abstraction.

You might also like