Java Second Unit Notes
Java Second Unit Notes
Java Applet
Applet is a special type of program that is embedded in the webpage to generate the dynamic
content. It runs inside the browser and works at client side.
Advantage of Applet
Hierarchy of Applet
o
Lifecycle of Java Applet
1. Applet is initialized.
2. Applet is started.
3. Applet is painted.
4. Applet is stopped.
5. Applet is destroyed.
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.
java.awt.Component class
1. public void paint(Graphics g): is used to paint the Applet. It provides Graphics
class object that can be used for drawing oval, rectangle, arc etc.
1. By html file.
2. By appletViewer tool (for testing purpose).
Note: class must be public because its object is created by Java Plugin software that resides on the browser.
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.
1
Component
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.
1
Label
A Label object is a component for placing text in a container.
2
Button
This class creates a labeled button.
3
Check Box
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.
6
Text Field
A TextField object is a text component that allows for the editing of a single line of text.
7
Text Area
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.
9
Canvas
A Canvas control represents a rectangular area where application can draw something or can
receive inputs created by user.
10
Image
An Image control is superclass for all image classes representing graphical images.
11
Scroll Bar
A Scrollbar control represents a scroll bar component in order to enable user to select from
range of values.
12
Dialog
A Dialog control represents a top-level window with a title and a border used to take some
form of input from the user.
13
File Dialog
A FileDialog control represents a dialog window from which the user can select a file.
AWT Layouts
Introduction
Layout means the arrangement of components within the container. In other way we can say that placing the
components at a particular position within the container. The task of layouting the controls is done automatically
by the Layout Manager.
Layout Manager
The layout manager automatically positions all the components within the container. If we do not use layout manager
then also the components are positioned by the default layout manager. It is possible to layout the controls by hand
but it becomes very difficult because of the following two reasons.
1
LayoutManager
The LayoutManager interface declares those methods which need to be
implemented by the class whose object will act as a layout manager.
2
LayoutManager2
The LayoutManager2 is the sub-interface of the LayoutManager.This
interface is for those classes that know how to layout containers based on
layout constraint object.
1
BorderLayout
The borderlayout arranges the components to fit in the five regions: east,
west, north, south and center.
2
CardLayout
The CardLayout object treats each component in the container as a card.
Only one card is visible at a time.
3
FlowLayout
The FlowLayout is the default layout.It layouts the components in a
directional flow.
4
GridLayout
The GridLayout manages the components in form of a rectangular grid.
5
GridBagLayout
This is the most flexible layout manager class.The object of GridBagLayout
aligns the component vertically,horizontally or along their baseline without
requiring the components of same size.
EventListner interface
It is a marker interface which every listener interface has to extend.This class is defined in
java.util package.
Class declaration
Following is the declaration for java.util.EventListener interface:
public interface EventListener
1
ActionListener
This interface is used for receiving the action events.
2
ComponentListener
This interface is used for receiving the component events.
3
ItemListener
This interface is used for receiving the item events.
4
KeyListener
This interface is used for receiving the key events.
5
MouseListener
This interface is used for receiving the mouse events.
6
TextListener
This interface is used for receiving the text events.
7
WindowListener
This interface is used for receiving the window events.
8
AdjustmentListener
This interface is used for receiving the adjusmtent events.
9
ContainerListener
This interface is used for receiving the container events.
10
MouseMotionListener
This interface is used for receiving the mouse motion events.
11
FocusListener
This interface is used for receiving the focus events.
Example
Live Demo
public class StringDemo {
public static void main(String args[]) {
char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.' };
String helloString = new String(helloArray);
System.out.println( helloString );
}
}
Output
hello.