I am creating a simple form for a class and I want the user to be able to press enter after filling out one JTextField
. It moves on to the next available JTextField
(not all of them are editable at a given time), similar to using tab.
When it gets to the last editable JTextField
it should submit the form. Is that possible or is tab and the usual enter function (as a submit my only option)?
I'll post the code if necessary, but if you can answer the question without it, that'd be great too.
Like Durandal mentioned, there are multiple ways to go about it.
You could use JTextField's addActionListener
method, addAction
method, addKeyListener
method, etc... They're all fairly similar - you're telling the text field to do something when something happens. (Although exactly how you do it differs by method)
Regardless, each time the action will need to know which text field is next in line. Then you can call JTextField#requestFocus()
to transfer input focus to it.
You can also do this with setFocusTraversalKeys
and setNextFocusableComponent