简体   繁体   中英

How to stop TextField from accepting input - AWT

I have a textField in which I input a username, and I want the screen to change when the 'enter' key is pressed. But what I noticed is that the KeyPressed method doesn't start, because the enter key is taken as input into the textfield.

Is there a way for me to loose focus on a TextField when the enter key is pressed, even if the KeyPressed method doesn't work while focused on the textfield?

You may use an ordinary event handler like this:

textField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        }
    });

A TextField action is different from a button action. A TextField action checks the Enter key for any actions, unlike a button, which checks for input through the mouse.

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