Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I listen to a TAB key pressed/typed in Java?

    private void jTextField1KeyPressed(java.awt.event.KeyEvent evt)     {         //cant capture my TAB?         System.out.print(evt.getKeyChar());      } 

What is the simplest way in an java gui to capture the tab key without doing using the focus listening technique?

like image 440
stackoverflow Avatar asked Nov 25 '11 23:11

stackoverflow


1 Answers

VK_TAB is the tab constant.

However: No Tab key-pressed or key-released events are received by the key event listener. This is because the focus subsystem consumes focus traversal keys, such as Tab and Shift Tab.

See: http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html

To solve this, apply the following to the component that is firing the key events (e.g., the TextArea):

.setFocusTraversalKeysEnabled(false)

Using this method, you must then handle focus traversal explicitly. Alternatively, the KeyEventDispatcher class can be used to pre-listen to all key events.

like image 196
Peter Avatar answered Oct 03 '22 19:10

Peter