Daniel Rad
Daniel Rad

Reputation: 38

How to get a KeyStroke working for a JLabel like it does for JButton?

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

Upvotes: 0

Views: 61

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347204

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");
                }
            });
        }

    }
}

Upvotes: 1

Related Questions