SlideShare a Scribd company logo
1 of 21
TextField and TextArea




http://improvejava.blogspot.in/
                                  1
Objective

On completion of this period, you would be
able to know

• TextField
• TextArea




              http://improvejava.blogspot.in/
                                                2
Recap

In the previous class, you have leant
• Lists
• Scroll bars




                http://improvejava.blogspot.in/
                                                  3
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
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
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
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
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
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
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
Handling a TextField                         contd..


• Here is the sample output




              Fig. 73.1 Output of TextFieldDemo




                   http://improvejava.blogspot.in/             11
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
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
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
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
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
Example Program                           contd..


• The output of TextAreaDemo is shown here




                  Fig. 73.2 Output of TextAreaDemo

                   http://improvejava.blogspot.in/             17
Summary

• We have discussed
  – TextField
  – TextArea components
  – The related program
• These components are useful for user text
  data entry



               http://improvejava.blogspot.in/   18
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
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
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

More Related Content

What's hot

class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
Constraints In Sql
Constraints In SqlConstraints In Sql
Constraints In Sql
Anurag
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 

What's hot (20)

Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
Array in c#
Array in c#Array in c#
Array in c#
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
 
Relational algebra ppt
Relational algebra pptRelational algebra ppt
Relational algebra ppt
 
Dbms 14: Relational Calculus
Dbms 14: Relational CalculusDbms 14: Relational Calculus
Dbms 14: Relational Calculus
 
Np cooks theorem
Np cooks theoremNp cooks theorem
Np cooks theorem
 
Prim's algorithm
Prim's algorithmPrim's algorithm
Prim's algorithm
 
Data structure ppt
Data structure pptData structure ppt
Data structure ppt
 
Nested Queries Lecture
Nested Queries LectureNested Queries Lecture
Nested Queries Lecture
 
class and objects
class and objectsclass and objects
class and objects
 
Dbms Notes Lecture 9 : Specialization, Generalization and Aggregation
Dbms Notes Lecture 9 : Specialization, Generalization and AggregationDbms Notes Lecture 9 : Specialization, Generalization and Aggregation
Dbms Notes Lecture 9 : Specialization, Generalization and Aggregation
 
Constraints In Sql
Constraints In SqlConstraints In Sql
Constraints In Sql
 
Trigger
TriggerTrigger
Trigger
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 
ORACLE PL/SQL
ORACLE PL/SQLORACLE PL/SQL
ORACLE PL/SQL
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Hash table in data structure and algorithm
Hash table in data structure and algorithmHash table in data structure and algorithm
Hash table in data structure and algorithm
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 

Similar to Text field and textarea

Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective c
Sunny Shaikh
 
Lists and scrollbars
Lists and scrollbarsLists and scrollbars
Lists and scrollbars
myrajendra
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rd
Connex
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
Connex
 
matmultHomework3.pdfNames of Files to Submit matmult..docx
matmultHomework3.pdfNames of Files to Submit  matmult..docxmatmultHomework3.pdfNames of Files to Submit  matmult..docx
matmultHomework3.pdfNames of Files to Submit matmult..docx
andreecapon
 
Chapter2pp
Chapter2ppChapter2pp
Chapter2pp
J. C.
 
New features and enhancement
New features and enhancementNew features and enhancement
New features and enhancement
Rakesh Madugula
 
Article link httpiveybusinessjournal.compublicationmanaging-.docx
Article link httpiveybusinessjournal.compublicationmanaging-.docxArticle link httpiveybusinessjournal.compublicationmanaging-.docx
Article link httpiveybusinessjournal.compublicationmanaging-.docx
fredharris32
 
Graphics, Threads and HTTPConnections in MIDLets
Graphics, Threads and HTTPConnections in MIDLetsGraphics, Threads and HTTPConnections in MIDLets
Graphics, Threads and HTTPConnections in MIDLets
Jussi Pohjolainen
 

Similar to Text field and textarea (20)

Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective c
 
Lists and scrollbars
Lists and scrollbarsLists and scrollbars
Lists and scrollbars
 
C programming day#3.
C programming day#3.C programming day#3.
C programming day#3.
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rd
 
Mobile Application Development class 007
Mobile Application Development class 007Mobile Application Development class 007
Mobile Application Development class 007
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to java
 
Developing a new Epsilon EMC driver
Developing a new Epsilon EMC driverDeveloping a new Epsilon EMC driver
Developing a new Epsilon EMC driver
 
Cocoa for Web Developers
Cocoa for Web DevelopersCocoa for Web Developers
Cocoa for Web Developers
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
 
matmultHomework3.pdfNames of Files to Submit matmult..docx
matmultHomework3.pdfNames of Files to Submit  matmult..docxmatmultHomework3.pdfNames of Files to Submit  matmult..docx
matmultHomework3.pdfNames of Files to Submit matmult..docx
 
Chapter2pp
Chapter2ppChapter2pp
Chapter2pp
 
java- Abstract Window toolkit
java- Abstract Window toolkitjava- Abstract Window toolkit
java- Abstract Window toolkit
 
iOS Application Development
iOS Application DevelopmentiOS Application Development
iOS Application Development
 
Intake 38 3
Intake 38 3Intake 38 3
Intake 38 3
 
New features and enhancement
New features and enhancementNew features and enhancement
New features and enhancement
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
 
I x scripting
I x scriptingI x scripting
I x scripting
 
Article link httpiveybusinessjournal.compublicationmanaging-.docx
Article link httpiveybusinessjournal.compublicationmanaging-.docxArticle link httpiveybusinessjournal.compublicationmanaging-.docx
Article link httpiveybusinessjournal.compublicationmanaging-.docx
 
2CPP15 - Templates
2CPP15 - Templates2CPP15 - Templates
2CPP15 - Templates
 
Graphics, Threads and HTTPConnections in MIDLets
Graphics, Threads and HTTPConnections in MIDLetsGraphics, Threads and HTTPConnections in MIDLets
Graphics, Threads and HTTPConnections in MIDLets
 

More from myrajendra (20)

Fundamentals
FundamentalsFundamentals
Fundamentals
 
Data type
Data typeData type
Data type
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc drivers
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1
 
Dao example
Dao exampleDao example
Dao example
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
 
Internal
InternalInternal
Internal
 
3. elements
3. elements3. elements
3. elements
 
2. attributes
2. attributes2. attributes
2. attributes
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
 
Headings
HeadingsHeadings
Headings
 
Forms
FormsForms
Forms
 
Css
CssCss
Css
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
 

Text field and textarea

  • 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