Focus on Keylistener

876 views Asked by At

So what I've got is that I would love to try and make an little login applet. I'm using Java Windowsbuilder for it, to make it easyer for me. I hope the code isn't to messy, because I'm just a starter.

The problem I've got is that My JButton "login" only registers a keyevent when it's selected trought tab, or when you first click it. What I want is that I can use the button always just by pressing the "ENTER" key.

Hope you guys solve my problem :)

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import org.eclipse.wb.swing.FocusTraversalOnArray;
import java.awt.Component;
import java.awt.event.KeyAdapter;

public class nummer1 extends JFrame{

private JPanel contentPane;
public static nummer1 theFrame;
private JTextField textFieldUsername;
private JTextField textFieldPass;
private nummer1  me;
private JLabel lblCheck;
private String password = "test", username = "test";

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                nummer1 frame = new nummer1();
                nummer1.theFrame = frame;
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
public void Check(){
    String Pass = textFieldPass.getText();
    String Username = textFieldUsername.getText();

    System.out.println(Pass);

    if (Pass.equals(password) && Username.equals(username)){

        lblCheck.setText("Correct Login");

    }else{

        lblCheck.setText("Invalid username or password");

    }
}



/**
 * Create the frame.
 */
public nummer1() {
    me = this;

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 356, 129);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);


    JLabel lblUsername = new JLabel("Username:");
    lblUsername.setBounds(10, 11, 61, 14);
    contentPane.add(lblUsername);

    JLabel lblPassword = new JLabel("Password:");
    lblPassword.setBounds(10, 36, 61, 14);
    contentPane.add(lblPassword);

    textFieldUsername = new JTextField();
    textFieldUsername.setBounds(81, 8, 107, 20);
    contentPane.add(textFieldUsername);
    textFieldUsername.setColumns(10);
    me.textFieldUsername = textFieldUsername;

    textFieldPass = new JTextField();
    textFieldPass.setBounds(81, 33, 107, 20);
    contentPane.add(textFieldPass);
    textFieldPass.setColumns(10);
    me.textFieldPass = textFieldPass;

    JButton btnLogin = new JButton("Login");
    contentPane.requestFocusInWindow();
    btnLogin.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            if(e.getKeyCode() == KeyEvent.VK_ENTER){
                me.Check();
                System.out.println("hi");
            }
        }
    });

    btnLogin.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            me.Check();
        }
    });
    btnLogin.setBounds(198, 7, 89, 23);
    contentPane.add(btnLogin);

    JLabel lblCheck = new JLabel("");
    lblCheck.setHorizontalAlignment(SwingConstants.TRAILING);
    lblCheck.setBounds(10, 65, 264, 14);
    contentPane.add(lblCheck);
    me.lblCheck = lblCheck;
    contentPane.setFocusTraversalPolicy(new FocusTraversalOnArray(new         
            Component[]{lblUsername, lblPassword, textFieldUsername, textFieldPass,   btnLogin, lblCheck}));

}
}

Thanks Emil!

2

There are 2 answers

0
camickr On

What I want is that I can use the button always just by pressing the "ENTER" key

Sounds like you want to make the "login" button the default button for the dialog.

See Enter Key and Button for the problem and solution.

0
user3466773 On

When using panels KeyListeners won't work unless the panel is focusable. What you can do is make a KeyBinding to the ENTER key using the InputMap and ActionMap for your panel.

public class Sample extends JPanel{
    //Code
    Sample() {
          //More code
          this.getInputMap().put(KeyStroke.getKeyStroke((char)KeyEvent.VK_ENTER), "Enter" );
          this.getActionMap().put("Enter", new EnterAction());
    }
    private class EnterAction extends AbstractAction(){
        @Override
        public void ActionPerformed(ActionEvent e){
            //Acion
        }
    }
}