requestFocusInWindow() swallows first key stroke in text field

I have been trying to move the focus to the next logical text field on my window when the user presses the ALT key.

I put the following code in the keyReleased event:

if event.keyCode == 18:

This will transfer the focus to the desired text field. However, the first character I type in the text field does not appear, but the second one does.

I have tried using “Invoke Later” under Advanced Settings on the event handler. I have even tried a double “Invoke Later” by using “Invoke Later” under Advanced Settings on the event handler AND using system.util.invokeLater() by using the following code:

if event.keyCode == 18:
	def focus():

I get the same behavior.

I also tried:

if event.keyCode == 18:
	nc = event.source.parent.getComponent('tfRptDt')
	nc.text = ' '
	nc.text = s

Just to see if changing the text in the text field would make a difference. The first character I type in the text field still disappears.

Any ideas?

Have you tried using the tab key without any requestFocusInWnindow() ? The tab key will swap fields left to right then top to bottom, but I believe you can customize the tab order by grouping components and get a different tab order that might work for you.


if event.keyCode == 18:
	target = event.source.parent.getComponent('tfRptDt')
	for listener in target.keyListeners:

Well, trying to use the default tab order wasn’t going to work, that’s why I ended up going this route. I didn’t know grouping would affect the default tab order, but it still wouldn’t work for me, because at least one move I need to make is up and to the right. Also, if I start grouping components now, a ton of existing scripting is going to need to change…way more than I want to deal with.

Paul - I copied your code in exactly and I am still getting the same behavior.

The issue stems from my use of the ALT key. I switched the key to CTRL+A and I don’t have the issue. My user seems to be satisfied with that, so I am going to leave it. I’m just going to make a note to avoid “special” keys.

1 Like

It was a guess - I figured the first component was ‘consuming’ the key event, so re-emitting it might work to get the second component to use it.