Adding an ActionListener to JTextField in Java

This tutorial will teach you how to create a program that has the ActionListener to JTextField in Java. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of txtFieldActionListener.java. 2. Import the following package library:
  1. import java.awt.event.*; //used to access the ActionEvent and ActionListener class
  2. import javax.swing.*; //used to access the JOptionPane,JFrame, and JTextField class
3. We will first put an extends keyword to have a JFrame because we will have a constructor. Then initialize variable in our Main, variable text as JTextField.
  1. public class txtFieldActionListener extends JFrame {
  2.   JTextField text = new JTextField("Press Enter", 40);
4. Next, we will create a constructor. Inside the constructor, we will add the actionlistener to the textfield so that when pressing the enter key it will popup "You pressed the Enter key!".
  1.   public txtFieldActionListener() {
  2.     text.addActionListener(new ActionListener() {
  3.       public void actionPerformed(ActionEvent e) {
  4.         JOptionPane.showMessageDialog(null, "You pressed the Enter key!");
  5.       }
  6.     });
Then add the textfield to the frame, set the title, and close the operation.
  1.     setTitle("Adding ActionListener to TextField");
  2.     getContentPane().add(text, "Center");
  3.     pack();
  4.     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
5. Now, in your Main, instantiate the constructor and set its visibility to true.
  1.   public static void main(String[] args) {
  2.     new txtFieldActionListener().setVisible(true);
  3.   }
Output: output Here's the full code of this tutorial:
  1. import java.awt.event.*; //used to access the ActionEvent and ActionListener class
  2. import javax.swing.*; //used to access the JOptionPane,JFrame, and JTextField class
  3.  
  4.  
  5. public class txtFieldActionListener extends JFrame {
  6.   JTextField text = new JTextField("Press Enter", 40);
  7.  
  8.   public txtFieldActionListener() {
  9.     text.addActionListener(new ActionListener() {
  10.       public void actionPerformed(ActionEvent e) {
  11.         JOptionPane.showMessageDialog(null, "You pressed the Enter key!");
  12.       }
  13.     });
  14.     setTitle("Adding ActionListener to TextField");
  15.     getContentPane().add(text, "Center");
  16.     pack();
  17.     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18.   }
  19.  
  20.   public static void main(String[] args) {
  21.     new txtFieldActionListener().setVisible(true);
  22.   }
  23. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment