JTextField is a part of javax.swing package. The class JTextField is a component that allows editing of a single line of text. JTextField inherits the JTextComponent class and uses the interface SwingConstants.
The constructor of the class are :
- JTextField() : constructor that creates a new TextField
- JTextField(int columns) : constructor that creates a new empty TextField with specified number of columns.
- JTextField(String text) : constructor that creates a new empty text field initialized with the given string.
- JTextField(String text, int columns) : constructor that creates a new empty textField with the given string and a specified number of columns .
- JTextField(Document doc, String text, int columns) : constructor that creates a textfield that uses the given text storage model and the given number of columns.
Methods of the JTextField are:
- setColumns(int n) :set the number of columns of the text field.
- setFont(Font f) : set the font of text displayed in text field.
- addActionListener(ActionListener l) : set an ActionListener to the text field.
- int getColumns() :get the number of columns in the textfield.
Following are the programs to implement JTextField.
1. Program to create a blank text field of definite number of columns.
output:
👁 Image
👁 Image
2. Program to create a blank text field with a given initial text and given number of columns
output :
👁 Image
👁 Image
3. Program to create a blank text field and set BOLD font type