Skip to main content

JAVA AWT : TextField, TextArea

Java AWT TextField

The object of a TextField class is a text component that allows the editing of a single line text. It inherits TextComponent class.

AWT TextField Class Declaration

  1. public class TextField extends TextComponent  

Java AWT TextField Example

  1. import java.awt.*;  
  2. class TextFieldExample{  
  3. public static void main(String args[]){  
  4.     Frame f= new Frame("TextField Example");  
  5.     TextField t1,t2;  
  6.     t1=new TextField("Welcome to Javatpoint.");  
  7.     t1.setBounds(50,100200,30);  
  8.     t2=new TextField("AWT Tutorial");  
  9.     t2.setBounds(50,150200,30);  
  10.     f.add(t1); f.add(t2);  
  11.     f.setSize(400,400);  
  12.     f.setLayout(null);  
  13.     f.setVisible(true);  
  14. }  
  15. }  

Java AWT TextField Example with ActionListener

  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. public class TextFieldExample extends Frame implements ActionListener{  
  4.     TextField tf1,tf2,tf3;  
  5.     Button b1,b2;  
  6.     TextFieldExample(){  
  7.         tf1=new TextField();  
  8.         tf1.setBounds(50,50,150,20);  
  9.         tf2=new TextField();  
  10.         tf2.setBounds(50,100,150,20);  
  11.         tf3=new TextField();  
  12.         tf3.setBounds(50,150,150,20);  
  13.         tf3.setEditable(false);   
  14.         b1=new Button("+");  
  15.         b1.setBounds(50,200,50,50);  
  16.         b2=new Button("-");  
  17.         b2.setBounds(120,200,50,50);  
  18.         b1.addActionListener(this);  
  19.         b2.addActionListener(this);  
  20.         add(tf1);add(tf2);add(tf3);add(b1);add(b2);  
  21.         setSize(300,300);  
  22.         setLayout(null);  
  23.         setVisible(true);  
  24.     }         
  25.     public void actionPerformed(ActionEvent e) {  
  26.         String s1=tf1.getText();  
  27.         String s2=tf2.getText();  
  28.         int a=Integer.parseInt(s1);  
  29.         int b=Integer.parseInt(s2);  
  30.         int c=0;  
  31.         if(e.getSource()==b1){  
  32.             c=a+b;  
  33.         }else if(e.getSource()==b2){  
  34.             c=a-b;  
  35.         }  
  36.         String result=String.valueOf(c);  
  37.         tf3.setText(result);  
  38.     }  
  39. public static void main(String[] args) {  
  40.     new TextFieldExample();  
  41. }  
  42. }  

Java AWT TextArea

The object of a TextArea class is a multi line region that displays text. It allows the editing of multiple line text. It inherits TextComponent class.

AWT TextArea Class Declaration

  1. public class TextArea extends TextComponent  

Java AWT TextArea Example

  1. import java.awt.*;  
  2. public class TextAreaExample  
  3. {  
  4.      TextAreaExample(){  
  5.         Frame f= new Frame();  
  6.             TextArea area=new TextArea("Welcome to javatpoint");  
  7.         area.setBounds(10,30300,300);  
  8.         f.add(area);  
  9.         f.setSize(400,400);  
  10.         f.setLayout(null);  
  11.         f.setVisible(true);  
  12.      }  
  13. public static void main(String args[])  
  14. {  
  15.    new TextAreaExample();  
  16. }  
  17. }  

Java AWT TextArea Example with ActionListener

  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. public class TextAreaExample extends Frame implements ActionListener{  
  4. Label l1,l2;  
  5. TextArea area;  
  6. Button b;  
  7. TextAreaExample(){  
  8.     l1=new Label();  
  9.     l1.setBounds(50,50,100,30);  
  10.     l2=new Label();  
  11.     l2.setBounds(160,50,100,30);  
  12.     area=new TextArea();  
  13.     area.setBounds(20,100,300,300);  
  14.     b=new Button("Count Words");  
  15.     b.setBounds(100,400,100,30);  
  16.     b.addActionListener(this);  
  17.     add(l1);add(l2);add(area);add(b);  
  18.     setSize(400,450);  
  19.     setLayout(null);  
  20.     setVisible(true);  
  21. }  
  22. public void actionPerformed(ActionEvent e){  
  23.     String text=area.getText();  
  24.     String words[]=text.split("\\s");  
  25.     l1.setText("Words: "+words.length);  
  26.     l2.setText("Characters: "+text.length());  
  27. }  
  28. public static void main(String[] args) {  
  29.     new TextAreaExample();  
  30. }  
  31. }  

Anurag Rana Educator CSE/IT

Comments

Popular posts from this blog

Standard and Formatted Input / Output in C++

The C++ standard libraries provide an extensive set of input/output capabilities which we will see in subsequent chapters. This chapter will discuss very basic and most common I/O operations required for C++ programming. C++ I/O occurs in streams, which are sequences of bytes. If bytes flow from a device like a keyboard, a disk drive, or a network connection etc. to main memory, this is called   input operation   and if bytes flow from main memory to a device like a display screen, a printer, a disk drive, or a network connection, etc., this is called   output operation . Standard Input and Output in C++ is done through the use of  streams . Streams are generic places to send or receive data. In C++, I/O is done through classes and objects defined in the header file  <iostream> .  iostream  stands for standard input-output stream. This header file contains definitions to objects like  cin ,  cout , etc. /O Library Header Files There are...

Genetic Algorithm: Population, Fitness Function, Parent Selection, Cross over, Mutation

Genetic Algo Population Population is a subset of solutions in the current generation. It can also be defined as a set of chromosomes. There are several things to be kept in mind when dealing with GA population − The diversity of the population should be maintained otherwise it might lead to premature convergence. The population size should not be kept very large as it can cause a GA to slow down, while a smaller population might not be enough for a good mating pool. Therefore, an optimal population size needs to be decided by trial and error. The population is usually defined as a two dimensional array of –  size population, size x, chromosome size . Population Initialization There are two primary methods to initialize a population in a GA. They are − Random Initialization  − Populate the initial population with completely random solutions. Heuristic initialization  − Populate the initial population using a known heuristic for the problem. It has been observed that the e...

Normalization in DBMS: 1NF, 2NF, 3NF and BCNF in Database

Normalization   is a process of organizing the data in database to avoid data redundancy, insertion anomaly, update anomaly & deletion anomaly.  Anomalies in DBMS There are three types of anomalies that occur when the database is not normalized. These are – Insertion, update and deletion anomaly. Let’s take an example to understand this. Example : Suppose a manufacturing company stores the employee details in a table named employee that has four attributes: emp_id for storing employee’s id, emp_name for storing employee’s name, emp_address for storing employee’s address and emp_dept for storing the department details in which the employee works. At some point of time the table looks like this: emp_id emp_name emp_address emp_dept 101 Nikhil Kangra D001 101 Nikhil Kangra D002 123 Ashish Shimla D890 166 Rahul Pathankot D900 166 Rahul Pathankot D004 The above table is not normalized.  Update anomaly : In the above table we have two rows for employee Nikhil as he belongs ...