Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


JTextField Overrides KeyListener


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Code Fox   Members   -  Reputation: 2267

Like
0Likes
Like

Posted 11 April 2014 - 09:13 PM

 I am trying to pass off whatever that is typed into the text field to a string whenever "enter" is pressed, however once something is typed into the JTextField, the KeyListener is automatically disabled !

 

 How can I have a text field, with a functional key listener at the same time ?


You Only Die Once !

~ Bobby Darin


Sponsor:

#2 Angex   Members   -  Reputation: 884

Like
2Likes
Like

Posted 12 April 2014 - 02:42 AM

If you only want to know when the enter key is pressed, you can use an action listener instead.

There is a good exmaple in the Oracle documentation.

 

In a nut-shell it's:

final JTextField textField = new JTextField(20);

textField.addActionListener(

  new ActionListener() {

    @Override
    public void actionPerformed(final ActionEvent evt) {

      // Enter pressed.
    }
  
  }
);

If you do need to capture other key strokes, consider using a DocumentListener.

KeyListeners can be troublesome when the registered component doesn't have focus.



#3 Glass_Knife   Moderators   -  Reputation: 6726

Like
0Likes
Like

Posted 15 April 2014 - 12:41 PM


If you do need to capture other key strokes, consider using a DocumentListener.
KeyListeners can be troublesome when the registered component doesn't have focus.

 

+1  Don't use a key listener for Swing Components.  They already handle all that for you, you just have to figure out what the magical recipe is...


I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS