You may be trying to solve the wrong problem caused by using a KeyListener
, which requires focus. Instead, use key bindings or setDefaultButton()
, found in the frame's root pane.
Addendum: I tried key bindings, and solved it almost. It still does not work when a JTextField
is selected. Do you maybe have a solution for that?
Depending on your needs, consider a DocumentListener
or DocumentFilter
. See this Q&A for more on the two.