tags:

views:

114

answers:

2

doesn't .getKeyCode( ) return the key's int value? Because i have set my jtextfield to listen to keylistener, and in the keytyped method, i check what key has been pressed. Here's a snippet of my code:

JTextField jtf = new JTextField( ); jtf.addKeyListener( this ); . . . public void keyTyped( KeyEvent e ) { if( e.getKeyCode( ) == KeyEvent.VK_ENTER ) System.out.println( "pressed enter" ); }

but everytime i type enter in the jtextfield, nothing happens, ie nothing prints.

A: 

maybe you should check first if you event handler is actually get called when you pressed anything... or if your event handler is able to receive KeyEvents objects, i believe the problem lies there... for it to work, the component must have focus... Java Tutorial

ultrajohn
yes the keyevent handler is responding, but how do i make my jtextfield respond to "enter" key events?
vamsi
A: 

I think you are comparing the wrong values. e.getKeyCode() returns the key, then i guess KeyEvent.VK_ENTER is something really different.

Halo