Java JTextField

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

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:

Constructor

Description

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:

Methods

Description

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

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