http://improvejava.blogspot.in/
TextField and TextArea
1
http://improvejava.blogspot.in/
On completion of this period, you would be able to know
• TextField• TextArea
2
Objective
http://improvejava.blogspot.in/
Recap
3
In the previous class, you have leant • Lists• Scroll bars
http://improvejava.blogspot.in/
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
4
http://improvejava.blogspot.in/
• 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
TextField contd..
5
http://improvejava.blogspot.in/
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
6
http://improvejava.blogspot.in/
• 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( )
TextField contd..
7
http://improvejava.blogspot.in/
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>
*/
8
http://improvejava.blogspot.in/
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 eventsname.addActionListener(this);pass.addActionListener(this);
}
Handling a TextField contd..
9
http://improvejava.blogspot.in/
// 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);
}}
Handling a TextField contd..
10
http://improvejava.blogspot.in/
• Here is the sample output
Handling a TextField contd..
Fig. 73.1 Output of TextFieldDemo
11
http://improvejava.blogspot.in/
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)
12
http://improvejava.blogspot.in/
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
13
http://improvejava.blogspot.in/
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)
14
http://improvejava.blogspot.in/
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 simple\n" +"that there are obviously no deficiencies.\n" +"And the other way is to make it so complicated\n" +"that there are no obvious deficiencies.\n\n" +" -C.A.R. Hoare\n\n" +
15
http://improvejava.blogspot.in/
"There's an old story about the person who wished\n" +"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.\n\n" +" -Bjarne Stroustrup, AT&T, (inventor of C++)";TextArea text = new TextArea(val, 10, 30);add(text);
}}
Example Program contd..
16
http://improvejava.blogspot.in/
• The output of TextAreaDemo is shown here
Fig. 73.2 Output of TextAreaDemo
Example Program contd..
17
http://improvejava.blogspot.in/
Summary
• We have discussed– TextField– TextArea components– The related program
• These components are useful for user text data entry
18
http://improvejava.blogspot.in/
Quiz
1. Which method TextField is used to show the effect of password
a) setPwdChar()
b) setEchoChar()
c) setPassword()
d) None
19
http://improvejava.blogspot.in/
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
20
http://improvejava.blogspot.in/
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
21