[go: up one dir, main page]

0% found this document useful (0 votes)
52 views11 pages

Java Second Unit Notes

The document discusses Java applets, their advantages, lifecycle, and how they are run. It also discusses AWT controls like labels, buttons, checkboxes, lists, text fields, canvases and images. It covers AWT layout managers like border, card, flow, and grid layouts. Finally, it discusses AWT event listeners like action, component, item, key, mouse, and window listeners.

Uploaded by

Dhwani Saxena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views11 pages

Java Second Unit Notes

The document discusses Java applets, their advantages, lifecycle, and how they are run. It also discusses AWT controls like labels, buttons, checkboxes, lists, text fields, canvases and images. It covers AWT layout managers like border, card, flow, and grid layouts. Finally, it discusses AWT event listeners like action, component, item, key, mouse, and window listeners.

Uploaded by

Dhwani Saxena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

JAVA unit 2nd 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

There are many advantages of applet. They are as follows:

o It works at client side so less response time.


o Secured
o It can be executed by browsers running under many platforms, including Linux, Windows,
Mac Os etc.

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.

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.

java.awt.Component class

The Component class provides 1 life cycle method of applet.

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.

How to run an Applet?

There are two ways to run an applet

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.

Sr. Control & Description


No.

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.

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.

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.

 It is very tedious to handle a large number of controls within the container.


 Oftenly the width and height information of a component is not given when we need to arrange them.
Java provide us with various layout manager to position the controls. The properties like size,shape and arrangement
varies from one layout manager to other layout manager. When the size of the applet or the application window
changes the size, shape and arrangement of the components also changes in response i.e. the layout managers
adapt to the dimensions of appletviewer or the application window.
The layout manager is associated with every Container object. Each layout manager is an object of the class that
implements the LayoutManager interface.
Following are the interfaces defining functionalities of Layout Managers.

Sr. Interface & Description


No.

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.

AWT Layout Manager Classes:


Following is the list of commonly used controls while designed GUI using AWT.

Sr. LayoutManager & Description


No.

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.

AWT Event Listeners


The Event listener represent the interfaces responsible to handle events. Java provides us
various Event listener classes but we will discuss those which are more frequently used. Every
method of an event listener method has a single argument as an object which is subclass of
EventObject class. For example, mouse event listener methods will accept instance of
MouseEvent, where MouseEvent derives from EventObject.

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

AWT Event Listener Interfaces:


Following is the list of commonly used event listeners.

Sr. No. Control & Description

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.

String Handling in Java


Strings, which are widely used in Java programming, are a sequence of characters. In Java programming
language, strings are treated as objects.
The Java platform provides the String class to create and manipulate strings.
The most direct way to create a string is to write −
String greeting = "Hello world!";
Whenever it encounters a string literal in your code, the compiler creates a String object with its value in
this case, "Hello world!'.

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.

A string is a collection of characters. In Java, a string is an object that represents a collection of


objects. A string is a predefined class used to create string objects. It is an immutable object,
which means it can’t be updated once created.

The string class has a set of built-in-methods, defined below.

 charAt(): It returns a character at a specified position.


 equals(): It compares the two given strings and returns a Boolean, that is, True or False.
 concat(): Appends one string to the end of another.
 length(): Returns the length of a specified string.
 toLowerCase(): Converts the string to lowercase letters.
 toUpperCase(): Converts the string to uppercase letters.
 indexOf(): Returns the first found position of a character.
 substring(): Extracts the substring based on index values, passed as an argument.

You might also like