0

I have a login form like this image.

enter image description here

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

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Daniel Rad
  • 38
  • 5
  • 2
    *"The login button it's a JLabel with an icon. "* .. ***Why?*** Or rather, why **not** use an undecorated `JButton` with an `ActionListener`? General tips: 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. The code in [this answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). – Andrew Thompson Jun 14 '21 at 10:37

1 Answers1

1

Hard to say "exactly" what's going on, as we only have a small, out of context, snippet, but I tend to avoid KeyStroke.getKeyStroke("ENTER") as it doesn't always produce an expected result. Instead, I make direct use KeyEvent virtual keys instead.

For example

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            add(new JTextField(10), gbc);
            add(new JPasswordField(10), gbc);

            add(new JLabel("..."), gbc);

            InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            ActionMap am = getActionMap();

            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "Login");
            am.put("Login", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent evt) {
                    System.out.println("Do stuff here");
                }
            });
        }

    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I've just done what you said and it does the same thing. It works only when I am with cursor in passwordField – Daniel Rad Jun 14 '21 at 09:19
  • 1
    @DanielRad The example I've provided works just fine for me - if it's not working for you then there is something else wrong with your code and you'll need to provide a [mcve] – MadProgrammer Jun 14 '21 at 10:43