Java Swing JTextField
Java Swing JTextField
The JTextField class object is a text component in which we can do the editing of a single line text.JTextField is a basic text control that lets the user enter a small amount text. It inherits JTextComponent class.
JTextField is generally used withGUI based data entry screens incommercial application or Applets.
Constructors used with JTextField
Type of Constructor | Constructor Description |
---|---|
JTextField() | This method creates a new TextField with no text and columns. |
JTextField(String text) | This method creates a new TextField which is initialized with the specified text. |
JTextField(int columns) | This method creates a new TextField without text but with the specified number of columns. . |
JTextField(String text, int columns) | This method creates a new TextField which is initialized with the specified text and specified number of columns. |
The following example creates a JTextField with Welcome to Compuhelp as the text in the text field.
Example
Basic Methods of JTextField class :
Method Name | Description |
---|---|
void setEditable(boolean) boolean isEditable() | Sets or gets whether the user can edit the text in the text field. |
void setColumns(int) int getColumns() | Set or get the number of columns displayed by the text field. |
void setText(String) | This method is used to set the specific text in the text field. |
void setHorizontalAlignment(int) int getHorizontalAlignment() | Set or get how the text is aligned horizontally within its area.One can use JTextField.LEFT,JTextField.CENTER,JTextField.RIGHT for aligning text in the text field. |
void addActionListener(ActionListener ae) | It is used to add the specified action listener to receive action events from this textfield. |
void setFont(Font f) | It is used to set the current font. |
void removeActionListener(ActionListener ae) | It is used to remove the specified action listener so that it no longer receives action events from this textfield. |
Example of JTextField with JTextField() and JTextField(String)
import javax.swing.*;
import java.awt.*;
{
JLabel lbl1;
JTextField txt1,txt2;
public TextFieldEx()
{
txt1=new JTextField("Swing Tutorial");
txt1.setBounds(100, 20, 150, 20);
add(txt1);
lbl1=new JLabel("Enter Name: ");
lbl1.setBounds(10, 50, 100, 20);
add(lbl1);
txt2=new JTextField();
txt2.setBounds(100, 50, 150, 20);
txt2.setToolTipText("Enter Name");
add(txt2);
setLayout(null);
setSize(350,300); setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class TextFieldExample
{
public static void main(String[] args)
{
TextFieldEx txt=new TextFieldEx();
}
}
The code above will display :
Example of Java Swing JTextField with ActionListener
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import static java.lang.Integer.parseInt;
class TextFieldEx extends JFrame implements ActionListener
{
JButton btn1,btn2;
JLabel lbl1,lbl2,lbl3 ;
JTextField txt1,txt2,txt3;
String str1,str2,str3;
int num1,num2,res;
public TextFieldEx()
{
lbl1=new JLabel(" Enter first number: ");
lbl2=new JLabel("Enter second number: ");
lbl3=new JLabel(" Result:");
btn1=new JButton("Sum");
btn2=new JButton("Clear");
txt1=new JTextField();
txt2=new JTextField();
txt3=new JTextField();
lbl1.setBounds(30, 40, 130, 50);
txt1.setBounds(180, 40, 200, 30);
lbl2.setBounds(30, 70, 130, 50);
txt2.setBounds(180, 80, 200, 30);
lbl3.setBounds(30, 110, 100, 50);
txt3.setBounds(180, 120, 200, 30);
btn1.setBounds(100, 180, 100, 30);
btn2.setBounds(210, 180, 100, 30);
add(lbl1);
add(txt1);
add(lbl2);
add(txt2);
add(lbl3);
add(txt3);
add(btn1);
add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
setTitle("Sum of Two Numbers");
setSize(450,300);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==btn1)
{
str1=txt1.getText();
str2=txt2.getText();
num1=parseInt(str1);
num2=parseInt(str2);
res=num1+num2;
txt3.setText(String.valueOf(res));
}
if (ae.getSource()==btn2)
{
txt1.setText("");
txt2.setText("");
txt3.setText("");
txt1.requestFocus();
}
}
}
public class TextFieldExample
{
public static void main(String[] args)
{
TextFieldEx text=new TextFieldEx();
}
}
The code above will display :