I have a peculiar requirement:
I have a Create New Object dialog box with several fields and OK and Cancel buttons. I want the OK button to have focus, so the user can simply open a dialog and press Enter to create a new object with default values. I tried calling requestFocusInWindow(), but this does not work until the window is shown. I cannot call it after the window is displayed, because the dialog is modal. And there is no type class setInitialFocusedComponent()in the dialog class.
OK, so I started creating KeyListenerfor each field in the dialog box (only 3 of them, without much attention), which will manually press the OK button if the user presses Enter. Now the problem is that the first field (and therefore focused) is JSpinnerthat which consumes its own KeyEvents. Therefore, pressing Enter does nothing.
How can I achieve this Input to OK behavior in my dialog without reorganizing the elements?
source
share