user2351205
user2351205

Reputation: 27

Using JFrame, how can I get the enter key to trigger the same result as pressing the button?

I've searched, but I haven't found an answer that I understand. Currently, I have found posts that ask questions about triggering all events and such, but I only need one event triggered. This code is for a login box.

Edit: The button that I need triggered by "ENTER" is the login button.

Here's my code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.IOException;
import java.io.Serializable;
/**
 * Write a description of class Login here.
 * 
 * @author () 
 * @version ()
*/
public class LoginFrame extends JFrame
{
private JTextField statusField = new JTextField(20);
private JTextField usernameField = new JTextField(10);
private String usernameText;
private JTextField passwordField = new JTextField(10);
private String passwordText;
private JButton loginButton = new JButton("Log in");
private JButton cancelButton = new JButton("Cancel");

public static void main() {
    LoginFrame app = new LoginFrame();
    app.setVisible(true);
    app.setLocationRelativeTo(null);
}

public LoginFrame() {
    super("Login");
    statusField.setText("Enter Username and Password");
    statusField.setHorizontalAlignment(JLabel.CENTER);
    statusField.setEditable(false);
    add(statusField, BorderLayout.NORTH);
    JPanel p = new JPanel();
    p.setLayout(new GridLayout(2, 2));
    p.add(new JLabel("User name:"));
    p.add(usernameField);
    usernameText = usernameField.getText();
    p.add(new JLabel("Password:"));
    p.add(passwordField);
    passwordText =passwordField.getText();
    add(p, BorderLayout.CENTER);
    Box buttonBar = Box.createHorizontalBox();
    buttonBar.add(Box.createHorizontalGlue());
    buttonBar.add(cancelButton);
    buttonBar.add(Box.createHorizontalGlue());
    buttonBar.add(loginButton);
    buttonBar.add(Box.createHorizontalGlue());
    add(buttonBar, BorderLayout.SOUTH);
    cancelButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent cancel) {
                System.exit(0);
            }
        });
    loginButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent login) {
                statusField.setText("Authenticating...");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    //Handle exception
                }
                if ((usernameText == "abc") && (passwordText == "123"))
                {
                    statusField.setText("Valid Username and Password");
                }
                else
                {
                    statusField.setText("Invalid: Locked Out");
                    usernameField.setEditable(false);
                    passwordField.setEditable(false);
                }

            }
        });
    p.setPreferredSize(new Dimension(335, 55));
    pack();
}

}

Upvotes: 1

Views: 1598

Answers (2)

Coutinho
Coutinho

Reputation: 1

//Right click on your jtextfield Proprieties/Events/KeyPressed

//Inside the event
int key = evt.getKeyCode();
if (key == KeyEvent.VK_ENTER) {
//Your Code

}//No Else

Upvotes: 0

A-SM
A-SM

Reputation: 884

Is it when you press Enter on username/password field the event will trigger? Just add ActionListener in those to fields, it will automatically trigger the event when you press Enter key.

Upvotes: 1

Related Questions