简体   繁体   中英

Hitting enter to move a to next text field, like tab key?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM