Advertisement Jump to content
Sign in to follow this  
RLS0812

JTextField Overrides KeyListener

This topic is 1736 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

 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 ?

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites


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...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!