Results 1 to 6 of 6
  1. #1
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    261
    Rep Power
    6

    Default KeyListener in JTextField not working

    I tried adding a KeyAdapter for a JTextField, but it never seems to fire the event. Any ideas as to what I'm doing wrong?

    Java Code:
    chat.addKeyListener(new ChatListener(this, message));

    Java Code:
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    
    import javax.swing.JTextField;
    
    public class ChatListener extends KeyAdapter
    {
    	private final NTFrame parent;
    	private final JTextField field;
    	
    	public ChatListener(NTFrame parent, JTextField field)
    	{
    		this.parent = parent;
    		this.field = field;
    	}
    	
    	@Override
    	public void keyReleased(KeyEvent e)
    	{
    		//System.out.println(e.getKeyCode());
    		if(e.getKeyCode() == KeyEvent.VK_ENTER)
    		{
    			String message = field.getText();
    			field.setText("");
    			parent.messageNormal("Test", message);
    			
    		}
    	}
    }
    Last edited by AndrewM16921; 12-10-2012 at 06:12 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: KeyListener in JTextField not working

    Why would you want to do this? Please tell us the details of the problem you are trying to solve with this. Who knows, perhaps you'd be better served using either a DocumentListener or a DocumentFilter.

  3. #3
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    261
    Rep Power
    6

    Default Re: KeyListener in JTextField not working

    Well, imagine an IRC client type of thing. I have a JTextField that users type their messages into. Then, when they hit enter it sends that message to be added to a JTextPane where I format it accordingly. I'm not that familiar with swing, so if there's a better way to do this please let me know! :)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: KeyListener in JTextField not working

    Quote Originally Posted by AndrewM16921 View Post
    Well, imagine an IRC client type of thing. I have a JTextField that users type their messages into. Then, when they hit enter it sends that message to be added to a JTextPane where I format it accordingly. I'm not that familiar with swing, so if there's a better way to do this please let me know! :)
    Yes, there is a much better way, and you would most definitely not want to use a KeyListener. Instead, just add an ActionListener to the JTextField, and the code in this will be called when the enter key is pressed.

  5. #5
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    261
    Rep Power
    6

    Default Re: KeyListener in JTextField not working

    Ok, thanks

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: KeyListener in JTextField not working

    You're welcome. As a general rule, with of course a few exceptions, you will want to avoid using KeyListeners with Swing GUI's.

Similar Threads

  1. Replies: 4
    Last Post: 08-17-2012, 10:38 PM
  2. KeyListener not working
    By cc11rocks in forum AWT / Swing
    Replies: 3
    Last Post: 02-09-2011, 01:09 PM
  3. keylistener + applet isn't working
    By brandon95 in forum Java Applets
    Replies: 6
    Last Post: 01-03-2011, 02:10 PM
  4. Replies: 0
    Last Post: 08-04-2009, 02:13 AM
  5. keyListener isn't working for me
    By lost_in_java in forum AWT / Swing
    Replies: 7
    Last Post: 12-05-2008, 04:24 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
  •