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.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 82
reverse digits of a number using for loop 5 52
Opening PDF on button click and fill new document 2 47
Java: The Public Class Main 4 30
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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 learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

808 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