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

JAVA Scrollbar, MenuItem and Menu, PopupMenu

ava AWT Scrollbar The  object  of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a  GUI  component allows us to see invisible number of rows and columns. AWT Scrollbar class declaration public   class  Scrollbar  extends  Component  implements  Adjustable, Accessible   Java AWT Scrollbar Example import  java.awt.*;   class  ScrollbarExample{   ScrollbarExample(){               Frame f=  new  Frame( "Scrollbar Example" );               Scrollbar s= new  Scrollbar();               s.setBounds( 100 , 100 ,  50 , 100 );               f.add(s);   ...

Difference between net platform and dot net framework...

Difference between net platform and dot net framework... .net platform supports programming languages that are .net compatible. It is the platform using which we can build and develop the applications. .net framework is the engine inside the .net platform which actually compiles and produces the executable code. .net framework contains CLR(Common Language Runtime) and FCL(Framework Class Library) using which it produces the platform independent codes. What is the .NET Framework? The Microsoft .NET Framework is a platform for building, deploying, and running Web Services and applications. It provides a highly productive, standards-based, multi-language environment for integrating existing investments with next-generation applications and services as well as the agility to solve the challenges of deployment and operation of Internet-scale applications. The .NET Framework consists of three main parts: the common language runtime, a hierarchical set of unified class librari...

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...