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,100, 200,30);  
  8.     t2=new TextField("AWT Tutorial");  
  9.     t2.setBounds(50,150, 200,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,30, 300,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...

Data Warehousing - An Overview

Anurag Information Technology (IT) has historically influenced organizational performance and competitive standing. The increasing processing power and sophistication of analytical tools and techniques have put the strong foundation for the product called data warehouse. There are a number of reasons that any organization should consider a data warehouse, which can be the critical tool for maximizing the organization’s investment in the information it has collected and stored throughout the enterprise. IT managers need to understand the rationale and benefits of data warehouses because they may need to design and implement, or procure this kingpin of business intelligence. The data warehouses are supposed to provide storage, functionality and responsiveness to queries beyond the capabilities of today's transaction-oriented databases. Also data warehouses are set to improve the data access performance of databases. Traditional databases balance the requirement of data access w...

Genotype Representation

Binary Representation This is one of the simplest and most widely used representation in GAs. In this type of representation the genotype consists of bit strings. For some problems when the solution space consists of Boolean decision variables – yes or no, the binary representation is natural. Take for example the 0/1 Knapsack Problem. If there are n items, we can represent a solution by a binary string of n elements, where the x th  element tells whether the item x is picked (1) or not (0). For other problems, specifically those dealing with numbers, we can represent the numbers with their binary representation. The problem with this kind of encoding is that different bits have different significance and therefore mutation and crossover operators can have undesired consequences. This can be resolved to some extent by using  Gray Coding,  as a change in one bit does not have a massive effect on the solution. Real Valued Representation For problems where we want to define ...