0% found this document useful (0 votes)
17 views

JTextfield

JTextfield
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

JTextfield

JTextfield
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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 Swi


ngConstants

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 Creates a new TextField initialized with the specified text
columns) and columns.

JTextField(int columns) Creates a new empty TextField with the specified number
of columns.
Commonly used Methods:

Methods Description

void addActionListener(ActionListener It is used to add the specified action listener to


l) 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 It is used to remove the specified action listener


removeActionListener(ActionListener l) 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,100, 200,30);
10. t2=new JTextField("AWT Tutorial");
11. t2.setBounds(50,150, 200,30);
12. f.add(t1); f.add(t2);
13. f.setSize(400,400);
14. f.setLayout(null);
15. f.setVisible(true);
16. }
17. }

Output:

You might also like