Results 1 to 2 of 2
  1. #1
    BoomPony is offline Member
    Join Date
    Nov 2010
    Rep Power

    Default Checking if enter has been pressed on a text field


    I'm trying to detect if enter has been pressed on a TextField, you can't type a newline in a TextField so its not as simple as

    Java Code:
    String s = tf.getText();
    if ( s.endsWith("\n") )
    so I'm using a KeyListener. In the KeyPressed event I tried doing this

    Java Code:
    public void keyPressed(KeyEvent arg0) {
        if ( arg0.equals( KeyEvent.VK_ENTER ) )
    but that doesn't seem to detect it.
    Any ideas?
    Thanks x


  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Rep Power


    By TextField I assume you mean JTextField? Add an actionListener to the JTextField.

Similar Threads

  1. Checking for multiple keys pressed
    By MagnusLarsen in forum New To Java
    Replies: 15
    Last Post: 08-22-2010, 11:51 AM
  2. Checking if a button was pressed in a Window.
    By Valkyrie in forum New To Java
    Replies: 2
    Last Post: 12-15-2009, 05:28 AM
  3. Detect the ENTER key being pressed
    By aaronfsimons in forum New To Java
    Replies: 12
    Last Post: 05-16-2009, 08:48 PM
  4. Regarding Text Field
    By adeeb in forum AWT / Swing
    Replies: 1
    Last Post: 06-05-2008, 11:01 PM
  5. Replies: 0
    Last Post: 01-28-2008, 09:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts