How to create a keyboard listener just for the ENTER key? [closed]

0

I'm working with a user registration window, and at the end I have the register button, but I want the user to press the enter key, do the same as if I clicked the register button, how can I do that? I have seen that the keyboard listeners work with all the keys but I only want for the enter key

    
asked by Christian 12.08.2018 в 03:58
source

1 answer

2

Since they are JTextField fields, all you have to do is add a listener.

myJTextField.addActionListener (new ActionListener () {

public void actionPerformed(ActionEvent e){

     //Lo que debería hacer

}});

this by pressing the enter will do what you put in the comment

You can also control the pressing of each key with the key listener, which in this case if you are only going to use the press, the adapter is recommended

myJTextField.addKeyListener(new KeyAdapter() {
    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_ENTER){
           //la acción a realizar
        }
    }

});
    
answered by 12.08.2018 в 04:20