1. A.V.C.COLLEGE OF ENGINEERING
MANNAMPANDAL, MAYILADUTHURAI-609 305
COURSE MATERIAL
FOR THE SUBJECT OF
PROGRAMMING PARADIGMS
SUB NAME : CS2305 PROGRAMMINGPARADIGMS
SEM : VI
DEPARTMENT : COMPUTER SCIENCE AND ENGINEERING
ACADEMIC YEAR : 2013-2013
NAME OF THE FACULTY : PARVATHI.M
DESIGNATION : Asst.Professor
1
2. A.V.C College of Engineering
Department of Computer Science & Engineering
2013 Odd Semester
SYLLABUS
CS2305 PROGRAMMING PARADIGMS L T P C
3 0 0 3
AIM:
To understand the concepts of object-oriented, event driven, and concurrent
programming paradigms and develop skills in using these paradigms using Java.
UNIT I OBJECT-ORIENTED PROGRAMMING – FUNDAMENTALS 9
Review of OOP - Objects and classes in Java – defining classes – methods - access
specifiers – static members – constructors – finalize method – Arrays – Strings -
Packages – JavaDoc comments
UNIT II OBJECT-ORIENTED PROGRAMMING – INHERITANCE 10
Inheritance – class hierarchy – polymorphism – dynamic binding – final keyword –
abstract classes – the Object class – Reflection – interfaces – object cloning – inner
classes – proxies
UNIT III EVENT-DRIVEN PROGRAMMING 10
Graphics programming – Frame – Components – working with 2D shapes – Using color,
fonts, and images - Basics of event handling – event handlers – adapter classes –
actions – mouse events – AWT event hierarchy – introduction to Swing – Model-View-
Controller design pattern – buttons – layout management – Swing Components
UNIT IV GENERIC PROGRAMMING 8
Motivation for generic programming – generic classes – generic methods – generic code
2
3. and virtual machine – inheritance and generics – reflection and generics – exceptions –
exception hierarchy – throwing and catching exceptions – Stack Trace Elements -
assertions – logging
UNIT V CONCURRENT PROGRAMMING 8
Multi-threaded programming – interrupting threads – thread states – thread properties –
thread synchronization – thread-safe Collections – Executors – synchronizers – threads
and event-driven programming
TOTAL=45 PERIODS
TEXT BOOK:
1. Cay S. Horstmann and Gary Cornell, “Core Java: Volume I – Fundamentals”, Eighth
Edition, Sun Microsystems Press, 2008.
REFERENCES:
1. K. Arnold and J. Gosling, “The JAVA programming language”, Third edition, Pearson
Education, 2000.
2. Timothy Budd, “Understanding Object-oriented programming with Java”, Updated
Edition, Pearson Education, 2000.
3. C. Thomas Wu, “An introduction to Object-oriented programming with Java”, Fourth
Edition, Tata McGraw-Hill Publishing company Ltd., 2006.
3
4. CS2305 PROGRAMMING PARADIGMSnit No Title
I Object-oriented Programming – Fundamentals
II Object-oriented Programming – Inheritance
III Event-Driven Programming
IV Generic Programming
V Concurrent Programming
4
7. CS2305 PROGRAMMING PARADIGMS
a) Encapsulation
Encapsulation is the mechanism that binds together code and the data it manipulates, and
keeps both safe from outside interference and misuse.
For example, shifting gears does not turn on the headlights in car, because it encapsulates
the information about all process.
In Java the basis of encapsulation is the class. A class defines the structure and behavior
(data and code) that will be shared by a set of objects. For this reason, objects are sometimes
referred to as instances of a class. Thus, a class is a logical construct and an object has physical
reality.
b) Inheritance
Inheritance is the process by which one object acquires the properties of another object.
This is important because it supports the concept of hierarchical classification. For example
Bird
Attributes:
Feathers
Lay eggs
Flying
bird
Attributes:
-----------
----------
Non flying
bird
Attributes:
-----------
-----------
The bird 'robin ' is a part of the class 'flying bird' which is again a part of the class 'bird'.
The concept of inheritance provides the idea of reusability.
c) Polymorphism
Polymorphism means the ability to take more than one form. Subclasses of a class can
define their own unique behaviors and yet share some of the same functionality of the parent
class.
Shape
Draw()
Circle Object
Draw()
Box Object
Draw()
Triangle Object
Draw()
10. CS2305 PROGRAMMING PARADIGMS
3. Language Basics
a) Comments
Java comments make your code easy to understand, modify and use. Java supports three
different types of comment styles.
1.Everything between initial slash –asterisk and ending asterisk-slash is ignored by the java
compiler.(/*……*/)
2.Double slash (//)mark is ignored by java compiler.
3.Everything between initial slash – asterisk - asterisk and ending asterisk-slash is ignored
by the java compiler and another program called JAVADOC.EXE that ships with the
JDK uses these comments to construct HTML documentation files that describe your
packages, classes and methods as well as all the variables used. (/**……*/)
b) Data Types
Java defines eight simple (or elemental) types of data: byte, short, int, long, char, float,
double, and boolean. These can be put in four groups:
• Integers - This group includes byte, short, int, and long, which are for whole valued
signed numbers.
• Floating -point numbers- This group includes float and double, which represent numbers
with fractional precision.
• Characters - This group includes char, which represents symbols in a character set, like
letters and numbers.
• Boolean - This group includes boolean, which is a special type for representing true/false
values.
c) Variables
Variables are locations in memory in which values can be stored. They have a name, a
type, and a value. Before you can use a variable, you have to declare it. After it is declared, you
can then assign values to it.
Java actually has three kinds of variables: instance variables, class variables, and local
variables.
• Instance variables are used to define attributes or the state for a particular object.
16. CS2305 PROGRAMMING PARADIGMS
default access level
When no access specifier is used, then by default the member of a class is public within
its own package, but cannot be accessed outside of its package.
7. Static Members
Static Variables
If you define a field as static, then there is only one such field per class. In contrast, each
object has its own copy of all instance fields. For example, let's suppose we want to assign a
unique identification number to each employee. We add an instance field id and a static field
nextId to the Employee class:
class Employee
{
...
private int id;
private static int nextId = 1;
}
Now, every employee object has its own id field, but there is only one nextId field that is shared
among all instances of the class. Let's put it another way. If there are one thousand objects of the
Employee class, then there are one thousand instance fields id, one for each object. But there is a
single static field nextId. Even if there are no employee objects, the static field nextId is present.
It belongs to the class, not to any individual object.
Constants
23. CS2305 PROGRAMMING PARADIGMS
b) Multidimensional Arrays
In Java, multidimensional arrays are actually arrays of arrays. These, as you might
expect, look and act like regular multidimensional arrays. However, as you will see, there are a
couple of subtle differences. To declare a multidimensional array variable, specify each
additional index using another set of square brackets. For example, the following declares a two-
dimensional array variable called twoD.
int twoD[][] = new int[4][5];
This allocates a 4 by 5 array and assigns it to twoD. Internally this matrix is implemented as an
array of arrays of int.
There is a second form that may be used to declare an array:
type[ ] var-name;
Here, the square brackets follow the type specifier, and not the name of the array variable. For
example, the following two declarations are equivalent:
int al[] = new int[3];
int[] a2 = new int[3];
The following declarations are also equivalent
11. String objects
The Java String class (java.lang.String) is a class of object that represents a character
array of arbitrary length. While this external class can be used to handle string objects, Java
integrates internal, built-in strings into the language.
25. CS2305 PROGRAMMING PARADIGMS
An important attribute of the String class is that once a string object is constructed, its
value cannot change (note that it is the value of an object that cannot change, not that of a string
variable, which is just a reference to a string object). All String data members are private, and no
string method modifies the string’s value.
String Methods
Although a string represents an array, standard array syntax cannot be used to inquire into
it. These are detailed in the below Table.
String Comparison
String comparison methods listed in below Table.
27. CS2305 PROGRAMMING PARADIGMS
String Searching
The String class also provides methods that search a string for the occurrence of a single
character or substring. These return the index of the matching substring or character if found, or -
1 if not found.
int indexOf (char ch)
int indexOf (char ch, int begin)
int lastIndexOf (char ch)
int lastIndexOf (char ch, int fromIndex)
int indexOf (String str)
int indexOf (String str, int begin)
int lastIndexOf (String str)
int lastIndexOf (String str, int fromIndex)
The following example shows the usage of these functions:
if (s1.indexOf (’:’) >= 0)
{
…
}
String suffix =
38. CS2305 PROGRAMMING PARADIGMS
Output
Hai
Hello
Methods labeled final, private, or static are not subject to dynamic lookup because they
may not be overridden. private methods are simply not inherited because they would never be
callable anyway. static methods apply to a particular class’s static data and thus make no sense in
a derivation. final methods are those designated as not-overridable for reasons of complexity or
safety.
public class Test1{
public static void main(String arg[]){
A a1=new A();
a1.display();
A a2=new B();
a2.display();
}
}
class A{
void display(){
System.out.println("Hai");
}
}
class B extends A{
void display(){
System.out.println("Hello");
}
}
Output
Hai
Hello
DYNAMIC BINDING
Selecting the appropriate method at runtime is called dynamic binding. Dynamic Binding
refers to the case where compiler is not able to resolve the call and the binding is done at runtime
only.
• All the instance methods in Java follow dynamic binding.
Dynamic binding has a very important property:
48. CS2305 PROGRAMMING PARADIGMS
Analyze the capabilities of classes at run time
Inspect objects at run time
Implement generic array manipulation code
Analyze the capabilities of classes at run time - Examine the structure of class.
Java.lang.Class
Field[] getFields()
Method[] getMethods()
Constructor[] getConstructors()
Return an array of public fields, methods
and constructors that the class supports. This
includes all public members of super class.
Field[] getDeclaredFields()
Method[] getDeclaredMethods()
Constructor[] getDeclaredConstructors()
Return an array of fields, methods and
constructors that are declared in the class.
This includes private, protected and public
members of class but not members of super
class.
Java.lang.reflect
Field Method Constructor
Methods (Field, Method & Constructor):
• String getName() – return the name of the item.
• int getModifiers()- return an integer, that describes the modifier.
Field
51. CS2305 PROGRAMMING PARADIGMS
public class MethodDemo{
public static void main(String args[])throws Exception{
Class c=Class.forName("Test");
Method m[]=c.getMethods();
Method mdec[]=c.getDeclaredMethods();
System.out.println("public Methods of class Test & its Super class:");
for(int i=0;i<m.length;i++)
System.out.println(m[i].getName());
System.out.println("All Methods:");
for(int i=0;i<mdec.length;i++)
System.out.println(mdec[i].getName());
}
}
Output:
public Methods of class Test & its Super
class:
method1
hashCode
getClass
wait
equals
notify
notifyAll
toString
All Methods:
method1
method2
method3
method4
56. CS2305 PROGRAMMING PARADIGMS
The proxy class can create brand-new classes at runtime. Such a proxy class implements
the interfaces that you specify. In particular, the proxy class has the following methods:
• All methods required by the specified interfaces;
• All methods defined in the Object class (toString, equals, and so on).
To create a proxy object, you use the newProxyInstance method of the Proxy class. The method has
three parameters:
1. A class loader. As part of the Java security model, it is possible to use different
class loaders for system classes, classes that are downloaded from the Internet,
and so on.
2. An array of Class objects, one for each interface to be implemented.
3. An invocation handler.
Proxies can be used for many purposes, such as:
• Routing method calls to remote servers;
• Associating user interface events with actions in a running program;
• Tracing method calls for debugging purposes.
61. CS2305 PROGRAMMING PARADIGMS
right, and positive y values are down. Coordinate units are measured in pixels (picture element).
All pixel values are integers; there are no partial or fractional pixels.
X coordinate: Horizontal distance moving right from the left of the screen.
Y coordinate: Vertical distance moving from top to bottom of the screen.
The Graphics class provides a set of simple built-in graphics primitives for drawing, including
lines, rectangles, polygons, ovals, and arcs.
Lines
To draw straight lines, use the drawLine method. drawLine takes four arguments: the x and y
coordinates of the starting point and the x and y coordinates of the ending point.
• void drawLine(int startX, int startY, int endX, int endY) - displays a line in the current
drawing color that begins at startX,startY and ends at endX,endY.
Example: Demo.java
import java.awt.*;
public class Demo extends Frame{
Demo(String s){
super(s);
setSize(100,100);
setVisible(true);
}
public void paint(Graphics g) {
g.drawLine(10,10,60,60);
}
public static void main(String arg[]){
Demo ob=new Demo("Line Demo");
}
}
63. CS2305 PROGRAMMING PARADIGMS
Rectangles
The Java graphics primitives provide two kinds of rectangles: Plain rectangles and Rounded
rectangles(which are rectangles with rounded corners).
• void drawRect(int x, int y, int width, int height)
• void fillRect(int x, int y, int width, int height)
• void drawRoundRect(int x, int y, int width, int height, int xDiam, int yDiam)
• void fillRoundRect(int x, int y, int width, int height, int xDiam, int yDiam)
A rounded rectangle has rounded corners. The upper-left corner of the rectangle is at x, y. The
dimensions of the rectangle are specified by width and height. The diameter of the rounding arc
along the X axis is specified by xDiam. The diameter of the rounding arc along the Y axis is
specified by yDiam.
Example: Demo.java
import java.awt.*; Demo ob=new Demo("Line Demo");
public class Demo extends Frame{
Demo(String s){
super(s);
setSize(500,500);
setVisible(true);
}
public void paint(Graphics g) {
g.drawRect(100,100,60,60);
g.fillRect(250,100,60,60);
g.drawRoundRect(100,250,60,60,10,10);
g.fillRoundRect(250,250,60,60,20,20);
}
public static void main(String arg[]){
}
}
Output:
65. CS2305 PROGRAMMING PARADIGMS
Polygons
Polygons are shapes with an unlimited number of sides. Set of x and y coordinates are needed to
draw a polygon, and the drawing method starts at one, draws a line to the second, then a line to
the third, and so on.
• void drawPolygon(int x[ ], int y[ ], int numPoints)
• void fillPolygon(int x[ ], int y[ ], int numPoints)
x[]- An array of integers representing x coordinates
y[]- An array of integers representing y coordinates
numPoints- An integer for the total number of points
Example: Demo.java
import java.awt.*;
public class Demo extends Frame{
public void paint(Graphics g) {
int x1[] = { 39,94,97,112,53,58,26 };
int y1[] = { 133,174,136,170,208,180,206 };
g.drawPolygon(x1,y1,7);
int x2[] = { 139,194,197,212,153,158,126 };
int y2[] = { 133,174,136,170,208,180,206 };
g.fillPolygon(x2,y2,7);
}
public static void main(String arg[]){
Demo ob=new Demo("Polygon Demo");
}
}
Output:
67. CS2305 PROGRAMMING PARADIGMS
Ovals
Use ovals to draw ellipses or circles.
• void drawOval(int top, int left, int width, int height)
• void fillOval(int top, int left, int width, int height)
69. CS2305 PROGRAMMING PARADIGMS
Example:Demo.java
import java.awt.*;
public class Demo extends Frame{
}
}
Demo(String s){
super(s);
setSize(300,300);
setVisible(true);
}
public void paint(Graphics g) {
g.drawOval(20,120,70,70);
g.fillOval(140,120,100,70);
}
public static void main(String arg[]){
Demo ob=new Demo("Oval Demo");
Arc
The arc is drawn from startAngle through the angular distance specified by arkAngle. Angles are
specified in degrees. The arc is drawn counterclockwise if sweepAngle is positive, and clockwise
if arkAngle is negative.
• void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
• void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
x=0
y=0
width=100
height=100
Startangle=90
arcAngle=180
73. Example:Demo.java
import java.awt.*;
public class Demo extends Frame{
Demo(String s){
super(s);
setSize(300,300);
setVisible(true);
CS2305 PROGRAMMING PARADIGMS
}
}
Output:
}
public void paint(Graphics g) {
g.drawArc(20,120,90,90,90,180);
g.fillArc(120,120,90,90,90,180);
g.drawArc(170,120,90,90,90,-180);
}
public static void main(String arg[]){
Demo ob=new Demo("Arc Demo");
Drawing Text
Draw text on the screen using the method drawstring().
• void drawstring(String text, int x, int y)- draws a string in the current font and color.
import java.awt.*;
public class Demo extends Frame{
Demo(String s){
super(s);
setSize(200,200);
setVisible(true);
}
public void paint(Graphics g) {
g.drawString("welcome", 75, 100);
}
public static void main(String arg[]){
Demo ob=new Demo("Text Demo");
}
}
78. CS2305 PROGRAMMING PARADIGMS
Methods
Java.awt.Graphics
• void setFont(Font fontObj)- selects a font for the graphics context. That font will be used
for subsequent text drawing operations.
• Font getFont( )- get the current font by calling getFont( ).
Java.awt.Component
• void setFont(Font fontObj)
Example: The following program that draws several lines of text in different fonts.
import java.awt.*;
public class Demo extends Frame{
Demo(String s){
super(s);setSize(200,200);setVisible(true);
}
public void paint(Graphics g) {
Font f = new Font("TimesRoman", Font.PLAIN, 18);
Font fb = new Font("TimesRoman", Font.BOLD, 18);
Font fi = new Font("TimesRoman", Font.ITALIC, 18);
Font fbi = new Font("TimesRoman", Font.BOLD + Font.ITALIC, 18);
g.setFont(f);
g.drawString("This is a plain font", 10, 50);
g.setFont(fb);
g.drawString("This is a bold font", 10, 75);
g.setFont(fi);
g.drawString("This is an italic font", 10, 100);
g.setFont(fbi);
g.drawString("This is a bold italic font", 10, 125);
public static void main(String arg[]){
Demo ob=new Demo("Font Demo");
}
}
Output:
}
Image: java.awt.Image
Image class provides support for displaying and manipulation of graphical images. Image is
simply a rectangular graphical object.
82. AWT Components
• All components are subclass of Component class
• Components allow the user to interact with application. A layout manager
arranges components within a container (Frame/Applet/Panel).
Adding and Removing Controls
• add(Component compObj)- add components to the conatainer. Once it is added, it will
automatically be visible whenever its parent window is displayed. Here, compObj is an
instance of the control that you want to add.
• void remove(Component obj)- remove a control from a window
84. CS2305 PROGRAMMING PARADIGMS
Scrollbars Scrollbar( )
Scrollbar(int style)
Scrollbar(int style, int initialValue, int thumbSize, int min, int max)
int getMinimum( )
int getMaximum( )
void setValues(int
initialValue, int thumbSize,
int min, int max)
Label
• Labels are components that hold text.
• Labels don’t react to user input. It is used to identify components.
Constructors
• Label(String str) - constructs a label with left-aligned text.
• Label(String str, int how) - constructs a label with the alignment specified by how.
Methods
• void setText(String str)- set the text in the label
• String getText( )- return the text of label
Example: The following example creates three labels and adds them to a frame..The labels are
organized in the frame by the flow layout manager.
import java.awt.*; Demo ob=new Demo("Label Demo");
public class Demo extends Frame{
Label lb1 = new Label("One");
Label lb2 = new Label("Two");
Label lb3 = new Label("Three");
FlowLayout flow= new FlowLayout();
Demo(String s){
super(s);
setSize(200,200);
setLayout(flow);
add(lb1);add(lb2);add(lb3);
setVisible(true);
}
public static void main(String arg[]){
87. CS2305 PROGRAMMING PARADIGMS
Button
A push button is a component that contains a label and that generates an event when it is pressed.
Push buttons are objects of type Button.
Constructors
• Button( )- creates an empty button
• Button(String str)- creates a button that contains str as a label.
Methods
• void setLabel(String str) -set the label in the button
• String getLabel( ) -return the label of button
Example: The following example creates three buttons and adds them to a frame. The buttons
are organized in the frame by the flow layout manager.
public static void main(String arg[]){
import java.awt.*; Demo ob=new Demo("Button Demo");
public class Demo extends Frame{
FlowLayout flow= new FlowLayout();
}}
Output:
Button b=new Button();
Button b1=new Button();
Button b2=new Button("Button 2");
Demo(String s){
super(s);setSize(200,200);
setLayout(flow);
b1.setLabel("Button 1");
add(b);add(b1);add(b2);
setVisible(true);
}
List
The List class provides a compact, multiple-choice, scrolling selection list. List object can be
constructed to display any number of choices in the visible window. It allows the user to select
multiple items.
Constructors
• List( )- allows to select one item at any one time
• List(int numRows)- the value of numRows specifies the number of entries in the list that
89. CS2305 PROGRAMMING PARADIGMS
will be visible always
• List(int numRows, boolean multipleSelect)- if multipleSelect is true, then the user may
select two or more items at a time
Method
• void add(String name)- Here, name is the name of the item added to the list. The first
form adds items to the end of the list.
• void add(String name, int index) -adds the item at the index specified by index
• String getSelectedItem( )- return the selected item
• String[ ] getSelectedItems( )- return the selected items.
Example: The following example creates a list and adds it to a frame.
import java.awt.*;
public class Demo extends Frame{
FlowLayout flow= new FlowLayout();
List l1=new List(2);
List l2=new List(3);
List l3=new List(4,true);
Label lb1 = new Label("Dept");
Label lb2 = new Label("Dept");
Label lb3 = new Label("Dept");
Demo(String s){
super(s);
setSize(200,300);
setLayout(flow);
l1.add("CSE");l1.add("ECE");l1.add("EEE");l1.add("MECH");
l2.add("CSE");l2.add("ECE");l2.add("EEE");l2.add("MECH");
l3.add("CSE");l3.add("ECE");l3.add("EEE");l3.add("MECH");
add(lb1);add(l1);add(lb2);add(l2);add(lb3);add(l3);
setVisible(true);
}
public static void main(String arg[]){
Demo ob=new
Demo("List Demo");
}
}
91. CS2305 PROGRAMMING PARADIGMS
CheckBox
A check box is a control that is used to turn an option on or off. It consists of a small box that can
either contain a check mark or not. There is a label associated with each check box that describes
what option the box represents.
Constructors
• Checkbox( )- check box whose label is initially blank
• Checkbox(String str)- check box whose label is specified by str.
• Checkbox(String str, boolean on) - allows you to set the initial state of the check box. If
on is true, the check box is initially checked
• Checkbox(String str, boolean on, CheckboxGroup cbGroup)- group is specified by
cbGroup
• Checkbox(String str, CheckboxGroup cbGroup, boolean on)
Methods
• boolean getState( )
• void setState(boolean on)
• String getLabel( )
• void setLabel(String str)
CheckboxGroup
Create a set of mutually exclusive check boxes in which one and only one check box in the group
can be checked at any one time. These check boxes are often called radio buttons. The default
constructor is defined, which creates an empty group.
Example: The following example creates a checkbox group (Gender) and checkboxes
(Languages Known).
import java.awt.*;
public class Demo extends Frame{
FlowLayout flow= new FlowLayout();
Label l1=new Label("Gender");
Label l2=new Label("Languages Known");
CheckboxGroup cg=new CheckboxGroup();
Checkbox c1=new Checkbox("Male",cg,true);
Checkbox c2=new Checkbox("Female",cg,false);
93. Checkbox c3=new Checkbox("VisualBasic");
Checkbox c4=new Checkbox("C++");
Checkbox c5=new Checkbox("Java");
Checkbox c6=new Checkbox("C");
CS2305 PROGRAMMING PARADIGMS
}
}
Output:
Demo(String s){
super(s);
setSize(200,200);
setLayout(flow);
add(l1);add(c1);add(c2);
add(l2);add(c3);add(c4);add(c5);add(c6);
setVisible(true);
}
public static void main(String arg[]){
Demo ob=new Demo("Checkbox Demo");
Choice
The Choice class is used to create a pop-up list of items from which the user may choose. It
allows the user to select single item at any time. Choice only defines the default constructor,
which creates an empty list.
To add a item to the list, call add( ). It has this general form:
• void add(String name)
• To determine which item is currently selected, you may call either getSelectedItem( ) or
getSelectedIndex( ).
import java.awt.*;
public class Demo extends Frame{
FlowLayout flow= new FlowLayout();
Label lb=new Label("City");
Choice c=new Choice();
Demo(String s){
super(s);
setSize(200,200);setLayout(flow);
c.add("Chennai");c.add("Coimbatore");
c.add("KanyaKumari");c.add("Madurai");
c.add("Tirunelveli");
add(lb);add(c);
setVisible(true);
}
public static void main(String arg[]){
Demo ob=new Demo("Checkbox Demo");
}
}
95. CS2305 PROGRAMMING PARADIGMS
TextField
Text fields allow the user to enter strings and to edit the text using the arrow keys, cut and paste
keys, and mouse selections.
• TextField( )- creates a default text field
• TextField(int numChars)- creates a text field that is numChars characters wide
• TextField(String str)- initializes the text field with the string contained in str
• TextField(String str, int numChars)
Methods
• String getText( )
• void setText(String str)
TextArea
Simple multiline editor allow the user to enter strings.
TextArea and TextField are subclass of TextComponent. Therefore, it supports the getText( ),
setText( ), getSelectedText( ), select( ), isEditable( ), and setEditable( ) methods described in
TextField class.
import java.awt.*;
public class Demo extends Frame{
FlowLayout flow= new FlowLayout();
Label lb1=new Label("Name");
Label lb2=new Label("No");
Label lb3=new Label("Message");
TextField t1=new TextField(20);
TextField t2=new TextField(15);