Browse Source

Made it so pressing enter on the keyboard activates the login and ok button

remotes/origin/server
Richard Schmidt 11 months ago
parent
commit
13d0a13392
  1. 27
      src/main/java/LoginGUI.java

27
src/main/java/LoginGUI.java

@ -2,6 +2,8 @@ import javax.swing.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.util.List; import java.util.List;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class LoginGUI extends JFrame implements ActionListener { public class LoginGUI extends JFrame implements ActionListener {
private JTextField usernameField; private JTextField usernameField;
@ -35,11 +37,18 @@ public class LoginGUI extends JFrame implements ActionListener {
loginButton.addActionListener(this); loginButton.addActionListener(this);
add(loginButton); add(loginButton);
getRootPane().setDefaultButton(loginButton);
} }
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) { if (e.getSource() == loginButton) {
login();
}
}
private void login() {
String username = usernameField.getText(); String username = usernameField.getText();
String password = new String(passwordField.getPassword()); String password = new String(passwordField.getPassword());
@ -49,9 +58,6 @@ public class LoginGUI extends JFrame implements ActionListener {
} else { } else {
JOptionPane.showMessageDialog(this, "Invalid username or password", "Login Error", JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(this, "Invalid username or password", "Login Error", JOptionPane.ERROR_MESSAGE);
} }
}
} }
// Function to authenticate the user by comparing the entered username and password with the saved user data // Function to authenticate the user by comparing the entered username and password with the saved user data
@ -68,6 +74,21 @@ public class LoginGUI extends JFrame implements ActionListener {
} }
private class EnterKeyListener implements KeyListener {
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
login();
}
}
@Override
public void keyReleased(KeyEvent e) {}
}
public static void main(String[] args) { public static void main(String[] args) {
SwingUtilities.invokeLater(() -> { SwingUtilities.invokeLater(() -> {
LoginGUI loginGUI = new LoginGUI(); LoginGUI loginGUI = new LoginGUI();

Loading…
Cancel
Save