JTextField Component In Java | Free Source Code


This is a tutorial I will going to teach you how to make a program that has the JTextField Component using Java. The JTextField is a basic text control component that enables the user to type a small amount of text.

So, now let’s start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of jTextFieldComponent.java.

2. Import the following packages:

JFrame frame = new JFrame("JTextField Component");
 
JLabel name = new JLabel("Enter name: ");
JTextField txtName = new JTextField(20);

We have built a JTextField component with variable named txtName. The 20 inside the JTextField components show the number of characters allowed on the text field.

To make the size of the textfield, we will use the setPreferredSize method and will access the Dimension class.

txtName.setPreferredSize(new Dimension(100, 30));

The width of the textfield dimension is 100 and the height is 30.

3. We will now add a KeyEvent and KeyListener to the textfield. We will be going to have a text field that when we input text in there, it will automatically convert into uppercase letters. Copy this code below:

txtName.addKeyListener(new KeyAdapter() {
  public void keyReleased(KeyEvent e) {
    JTextField textField = (JTextField) e.getSource();
    String text = textField.getText();
    textField.setText(text.toUpperCase());
  }

We have created a text variable for our TextField. The getText method gets the string input of the user. The setText method will set the text to the textfield and will be converted into uppercase letters using toUpperCase method.

4. Then, add the JLabel variable to the frame using the default BorderLayout of West position and the JTextField variable using the default BorderLayout of South position of the getContentPane method. Finally, set the size, visibility, and the close operation of the frame. Copy this code below:

frame.getContentPane().add(name,"West");
frame.getContentPane().add(txtName,"South");
 frame.setSize(400, 200);
 frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Output:

output


Here’s the full code of this tutorial:

import java.awt.*; //used to access the Dimension class
import java.awt.event.*; //used to access the KeyAdapter and KeyEvent class
import javax.swing.*; //used to access the JFrame, JLabel, and JTextField class
 
 
public class jTextFieldComponent {
 
  public static void main(String[] args) {
  	JFrame frame = new JFrame("JTextField Component");
 
 
    JLabel name = new JLabel("Enter name: ");
    JTextField txtName = new JTextField(20);
 
    txtName.setPreferredSize(new Dimension(100, 30));
 
 
    txtName.addKeyListener(new KeyAdapter() {
      public void keyReleased(KeyEvent e) {
        JTextField textField = (JTextField) e.getSource();
        String text = textField.getText();
        textField.setText(text.toUpperCase());
      }
 
 
    });
    frame.getContentPane().add(name,"West");
    frame.getContentPane().add(txtName,"South");
     frame.setSize(400, 200);
     frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
  }
}



Sharing is caring!

Facebook Comments

Leave a Reply