Results 1 to 6 of 6
- 12-10-2012, 05:53 PM #1
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 183
- Rep Power
- 5
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.
-
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.
- 12-11-2012, 01:24 AM #3
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 183
- Rep Power
- 5
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! :)
-
Re: KeyListener in JTextField not working
- 12-11-2012, 01:45 AM #5
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 183
- Rep Power
- 5
Re: KeyListener in JTextField not working
Ok, thanks
-
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
-
Keylistener not working when screen is constantly refreshed
By evared in forum AWT / SwingReplies: 4Last Post: 08-17-2012, 10:38 PM -
KeyListener not working
By cc11rocks in forum AWT / SwingReplies: 3Last Post: 02-09-2011, 01:09 PM -
keylistener + applet isn't working
By brandon95 in forum Java AppletsReplies: 6Last Post: 01-03-2011, 02:10 PM -
JPopupMenu, JTextField, and Canvas not working together
By ShinobiAC in forum AWT / SwingReplies: 0Last Post: 08-04-2009, 02:13 AM -
keyListener isn't working for me
By lost_in_java in forum AWT / SwingReplies: 7Last Post: 12-05-2008, 04:24 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks