Skip to main content

Java Swing: JTextField, JtextArea.

Java JTextField

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

JTextField class declaration

Let's see the declaration for javax.swing.JTextField class.

  1. public class JTextField extends JTextComponent implements SwingConstants  

Commonly used Constructors:

ConstructorDescription
JTextField()Creates a new TextField
JTextField(String text)Creates a new TextField initialized with the specified text.
JTextField(String text, int columns)Creates a new TextField initialized with the specified text and columns.
JTextField(int columns)Creates a new empty TextField with the specified number of columns.

Commonly used Methods:

MethodsDescription
void addActionListener(ActionListener l)It is used to add the specified action listener to receive action events from this textfield.
Action getAction()It returns the currently set Action for this ActionEvent source, or null if no Action is set.
void setFont(Font f)It is used to set the current font.
void removeActionListener(ActionListener l)It is used to remove the specified action listener so that it no longer receives action events from this textfield.

Java JTextField Example

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

Output:

JAVA Jtextfield 1

Java JTextField Example with ActionListener

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

Output:

JAVA Jtextfield 2

Java JTextArea

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

JTextArea class declaration

Let's see the declaration for javax.swing.JTextArea class.

  1. public class JTextArea extends JTextComponent  

Commonly used Constructors:

ConstructorDescription
JTextArea()Creates a text area that displays no text initially.
JTextArea(String s)Creates a text area that displays specified text initially.
JTextArea(int row, int column)Creates a text area with the specified number of rows and columns that displays no text initially.
JTextArea(String s, int row, int column)Creates a text area with the specified number of rows and columns that displays specified text.

Commonly used Methods:

MethodsDescription
void setRows(int rows)It is used to set specified number of rows.
void setColumns(int cols)It is used to set specified number of columns.
void setFont(Font f)It is used to set the specified font.
void insert(String s, int position)It is used to insert the specified text on the specified position.
void append(String s)It is used to append the given text to the end of the document.

Java JTextArea Example

  1. import javax.swing.*;  
  2. public class TextAreaExample  
  3. {  
  4.      TextAreaExample(){  
  5.         JFrame f= new JFrame();  
  6.         JTextArea area=new JTextArea("Welcome to javatpoint");  
  7.         area.setBounds(10,30200,200);  
  8.         f.add(area);  
  9.         f.setSize(300,300);  
  10.         f.setLayout(null);  
  11.         f.setVisible(true);  
  12.      }  
  13. public static void main(String args[])  
  14.     {  
  15.    new TextAreaExample();  
  16.     }}  

Output:

JAVA Jtextarea 1

Java JTextArea Example with ActionListener

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

Output:

JAVA Jtextarea 2


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);               f.setSize( 400 , 400 );               f.setLayout( null );               f.setVisible( true );   }   public   static   void  main(String args[]){           new  ScrollbarExample();   }   }   Output: Java AWT Scrollbar Example with AdjustmentListener import  java.awt.*;   import  java.awt.event.*;   class  ScrollbarExample{        ScrollbarExample(){               Frame f=  new  Frame( "Scro

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 following header files important to C++ pro