Solved

To Capture 'Enter Key' on a JTextField

Posted on 2001-08-03
6
3,528 Views
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

Bijoy
0
Comment
Question by:bijoyn
  • 3
  • 2
6 Comments
 
LVL 1

Accepted Solution

by:
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();
        frm.getContentPane().setLayout(null);
        frm.addWindowListener(
            new WindowAdapter()
            {
                public void windowClosing( WindowEvent e )
                {
                    ((JFrame)e.getSource()).dispose();
                    System.exit(0);
                }
            }
        );
       
        JTextField jtf = new JTextField();
        jtf.setBounds(10, 10, 200, 30);
        jtf.setVisible(true);
       
        jtf.addKeyListener( new ifEnterKeyPressed() );
       
        frm.getContentPane().add(jtf);
        frm.setBounds(100, 100, 400, 400);
       
        frm.show();
    }
}

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) {}
}

0
 

Author Comment

by:bijoyn
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

Bijoy
0
 
LVL 92

Expert Comment

by:objects
ID: 6350525
You don't need a key listener.
An action event is fired when the <Enter> key is pressed.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Expert Comment

by:objects
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());
}
0
 
LVL 1

Expert Comment

by:giamtrot
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

0
 
LVL 92

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.

910 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now