To Capture 'Enter Key' on a JTextField

Posted on 2001-08-03
Last Modified: 2007-12-19
I want my program to perform a piece of code if I type something in a JTextfield and hit enter. How do I do it.

Examples would be appreciated.

Thanx In Advance

Question by:bijoyn
  • 3
  • 2

Accepted Solution

giamtrot earned 100 total points
ID: 6349097
I think this should work:

import java.awt.event.*;
import javax.swing.*;

public class enter
    public static void main(String args[])
        JFrame frm = new JFrame();
            new WindowAdapter()
                public void windowClosing( WindowEvent e )
        JTextField jtf = new JTextField();
        jtf.setBounds(10, 10, 200, 30);
        jtf.addKeyListener( new ifEnterKeyPressed() );
        frm.setBounds(100, 100, 400, 400);

class ifEnterKeyPressed implements KeyListener
    public void keyPressed(KeyEvent e)
        if ( e.getKeyCode() == KeyEvent.VK_ENTER )
            System.out.println("It works.");
    public void keyReleased(KeyEvent e) {}
    public void keyTyped(KeyEvent e) {}


Author Comment

ID: 6349859
I guess I was doing the same thing. Can u please tell me how to assign the value of the JTextField to a variable. I would then go ahead and accept your answer.

Thanks a million

LVL 92

Expert Comment

ID: 6350525
You don't need a key listener.
An action event is fired when the <Enter> key is pressed.
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

LVL 92

Expert Comment

ID: 6350532
tf.addActionListener(new ActionListener()
   { public void actionPerformed(ActionEvent e) { enter(e); }});


private void enter(ActionEvent e)
   JTextField tf = (JTextField) e.getSource();
   System.out.println("Entered text: "+tf.getText());

Expert Comment

ID: 6351045
The comment from objects shows out to get the text from the object:

you can take a reference to the object calling out of the event ( e.getSource() ) and then cast the generic object you get to a JTextField.
Now you have a full reference to the JTextField

LVL 92

Expert Comment

ID: 6354379
It also shows that you don't need a key listener to know when the <Enter> is pressed :)

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 96
thymeleaf natural templating vs JSP 2 99
fibonacci ten numbers 4 38
Adding multiple JVM environments to RedHat 6 7 21
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question