Adding an ActionListener to JTextField in Java
Submitted by donbermoy on Wednesday, December 17, 2014 - 00:23.
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:
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.
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!".
Then add the textfield to the frame, set the title, and close the operation.
5. Now, in your Main, instantiate the constructor and set its visibility to true.
Output:
Here's the full code of this tutorial:
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
- import java.awt.event.*; //used to access the ActionEvent and ActionListener class
- import javax.swing.*; //used to access the JOptionPane,JFrame, and JTextField class
- public txtFieldActionListener() {
- }
- });
- setTitle("Adding ActionListener to TextField");
- getContentPane().add(text, "Center");
- pack();
- new txtFieldActionListener().setVisible(true);
- }
- import java.awt.event.*; //used to access the ActionEvent and ActionListener class
- import javax.swing.*; //used to access the JOptionPane,JFrame, and JTextField class
- public txtFieldActionListener() {
- }
- });
- setTitle("Adding ActionListener to TextField");
- getContentPane().add(text, "Center");
- pack();
- }
- new txtFieldActionListener().setVisible(true);
- }
- }
Add new comment
- 269 views