2. Objective
On completion of this period, you would be
able to know
• TextField
• TextArea
http://improvejava.blogspot.in/
2
3. Recap
In the previous class, you have leant
• Lists
• Scroll bars
http://improvejava.blogspot.in/
3
4. TextField
• The TextField class implements a single-line
text-entry area
• It is usually called an edit control
• Text fields allow the user
– to enter strings
– to edit the text using the arrow keys, cut and
paste keys, and mouse selections
• TextField is a subclass of TextComponent
http://improvejava.blogspot.in/ 4
5. TextField contd..
• TextField defines the following constructors:
– TextField( )
– TextField(int numChars)
– TextField(String str)
– TextField(String str, int numChars)
– The first version creates a default text field
– The second form creates a text field that is numChars
characters wide
– The third form initializes the text field with the string
contained in str
– The fourth form initializes a text field and sets its
width
http://improvejava.blogspot.in/ 5
6. TextField contd..
• To obtain the string
currently contained in the
text field, call getText( )
• To set the text, call
setText( )
• These methods are as
follows
– String getText( )
– void setText(String str)
– Here, str is the new
string
http://improvejava.blogspot.in/ 6
7. TextField contd..
• The other methods of interest are
– String getSelectedText( )
– void select(int startIndex, int endIndex)
– boolean isEditable( )
– void setEditable(boolean canEdit)
– void setEchoChar(char ch)
– boolean echoCharIsSet( )
– char getEchoChar( )
http://improvejava.blogspot.in/ 7
8. Handling a TextField
• Let us see an example that creates the
classic user name and password screen
// Demonstrate text field.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="TextFieldDemo" width=380 height=150>
</applet>
*/
http://improvejava.blogspot.in/ 8
9. Handling a TextField contd..
public class TextFieldDemo extends Applet implements ActionListener {
TextField name, pass;
public void init() {
Label namep = new Label("Name: ", Label.RIGHT);
Label passp = new Label("Password: ", Label.RIGHT);
name = new TextField(12);
pass = new TextField(8);
pass.setEchoChar('?');
add(namep);
add(name);
add(passp);
add(pass);
// register to receive action events
name.addActionListener(this);
pass.addActionListener(this);
}
http://improvejava.blogspot.in/ 9
10. Handling a TextField contd..
// User pressed Enter.
public void actionPerformed(ActionEvent ae) {
repaint();
}
public void paint(Graphics g) {
g.drawString("Name: " + name.getText(), 6, 60);
g.drawString("Selected text in name: "
+ name.getSelectedText(), 6, 80);
g.drawString("Password: " + pass.getText(), 6, 100);
}
}
http://improvejava.blogspot.in/ 10
11. Handling a TextField contd..
• Here is the sample output
Fig. 73.1 Output of TextFieldDemo
http://improvejava.blogspot.in/ 11
12. TextArea
• Sometimes a single line of text input is not enough
for a given task
• To handle these situations, the AWT includes a
simple multiline editor called TextArea
• Following are the constructors for TextArea
– TextArea( )
– TextArea(int numLines, int numChars)
– TextArea(String str)
– TextArea(String str, int numLines, int numChars)
– TextArea(String str, int numLines, int numChars, int Bars)
http://improvejava.blogspot.in/ 12
13. TextArea contd..
• Here,
– numLines specifies the height, in lines, of the
text area
– numChars specifies its width, in characters
– Initial text can be specified by str
– In the fifth form you can specify the scroll bars
that you want the control to have
http://improvejava.blogspot.in/ 13
14. TextArea contd..
• TextArea adds the following methods
– void append(String str)
– void insert(String str, int index)
– void replaceRange(String str, int startIndex,
int endIndex)
http://improvejava.blogspot.in/ 14
15. Example Program
// Demonstrate TextArea.
import java.awt.*;
import java.applet.*;
/*
<applet code="TextAreaDemo" width=300 height=250>
</applet>
*/
public class TextAreaDemo extends Applet {
public void init() {
String val = "There are two ways of constructing " +
"a software design.n" +
"One way is to make it so simplen" +
"that there are obviously no deficiencies.n" +
"And the other way is to make it so complicatedn" +
"that there are no obvious deficiencies.nn" +
" -C.A.R. Hoarenn" +
http://improvejava.blogspot.in/ 15
16. Example Program contd..
"There's an old story about the person who wishedn" +
"his computer were as easy to use as his telephone.n" +
"That wish has come true,n" +
"since I no longer know how to use my telephone.nn" +
" -Bjarne Stroustrup, AT&T, (inventor of C++)";
TextArea text = new TextArea(val, 10, 30);
add(text);
}
}
http://improvejava.blogspot.in/ 16
17. Example Program contd..
• The output of TextAreaDemo is shown here
Fig. 73.2 Output of TextAreaDemo
http://improvejava.blogspot.in/ 17
18. Summary
• We have discussed
– TextField
– TextArea components
– The related program
• These components are useful for user text
data entry
http://improvejava.blogspot.in/ 18
19. Quiz
1. Which method TextField is used to show the
effect of password
a) setPwdChar()
b) setEchoChar()
c) setPassword()
d) None
http://improvejava.blogspot.in/ 19
20. Quiz contd..
2. Which component is useful for entering the
address details(as a single unit) of a person ?
a) TextField
b) TextArea
c) Label
d) List
http://improvejava.blogspot.in/ 20
21. Frequently Asked Questions
1. List and explain the methods of TextField
2. List the different constructors of TextArea
3. Write a Java program to accept the details of
student like, PIN, Name, branch etc., in
TextFields and show the same in labels
http://improvejava.blogspot.in/ 21