JTextField and the Enter Key

N

Nuggy

I have the following issue, condensed down to this simple example:

Data entry program. Screen contains three JTextFields. I want the
program to automatically jump to the next field when the user presses
"Enter", instead of the user having to press Tab. (We have picky Data
Entry people.)

I am performing validation on these fields when they lose focus, by
means of focusGained and focusLost methods from the implemented
FocusListener class.

I have buttons that I have been able to "trigger" when they have focus
and the user presses the enter key; I do not want to lose this
functionality.

This is how I implemented this for the buttons, but doing the same for
the text fields does not seem to work:

button.registerKeyboardAction(
button.getActionForKeyStroke(
KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true)),
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true),
JComponent.WHEN_FOCUSED);

If anyone can let me know how to accomplish this, I'd appreciate it!

TIA,

-Mike
 
K

Knute Johnson

Nuggy said:
I have the following issue, condensed down to this simple example:

Data entry program. Screen contains three JTextFields. I want the
program to automatically jump to the next field when the user presses
"Enter", instead of the user having to press Tab. (We have picky Data
Entry people.)

I am performing validation on these fields when they lose focus, by
means of focusGained and focusLost methods from the implemented
FocusListener class.

I have buttons that I have been able to "trigger" when they have focus
and the user presses the enter key; I do not want to lose this
functionality.

This is how I implemented this for the buttons, but doing the same for
the text fields does not seem to work:

button.registerKeyboardAction(
button.getActionForKeyStroke(
KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true)),
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true),
JComponent.WHEN_FOCUSED);

If anyone can let me know how to accomplish this, I'd appreciate it!

TIA,

-Mike

I haven't tried it but I would think you could register an
ActionListener on your JTextField and transferFocus.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top