I have a login form like this image.
The login button it's a JLabel with an icon. I want to be able to press "ENTER" and login.
I've put the login logic into a method called doAction(); I have a MouseEvent on login Label that is working as expected and calls doAction() anytime I click on login Jlabel. I want the same thing to happen anytime I press "ENTER". I tried this:
KeyStroke keyStroke = KeyStroke.getKeyStroke("ENTER");
int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = login.getInputMap(condition);
ActionMap actionMap = login.getActionMap();
inputMap.put(keyStroke, keyStroke.toString());
actionMap.put(keyStroke.toString(), new AbstractAction() {
@Override
public void actionPerformed(ActionEvent arg0) {
doAction();
}
});
The problem is that this is working only if I am with cursor in passwordField. If I am with cursor in usernameField it's not working