I'm currently working on a java application and I just made a login frame. It works fine but since I have to login everytime when I want to acces the second JFrame it occured to me that it is kind of irritating that you can't log in by pressing enter on the password text field. Is there a way to make the textfield use the same actionlistner as the button?
Here is the code I'm currently using. Feel free to use it for your own log in system!
package presentation;
/**
*
* @author Jessie den Ridder
*/
import javax.swing.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import presentation.ScreenInfoFrame;
public class MyLogin {
private JFrame frame = new JFrame("Login");
private final JLabel inputLabel1 = new JLabel("Gebruikersnaam");
private final JLabel inputLabel2 = new JLabel("Wachtwoord");
private JTextField input1 = new JTextField();
private JPasswordField input2 = new JPasswordField();
private final JButton button = new JButton("Login");
private final JLabel inputLabel3 = new JLabel("");
public MyLogin() {
inputLabel1.setBounds(850, 405, 180, 20);
input1.setBounds(1000, 400, 180, 30);
inputLabel2.setBounds(850, 455, 180, 20);
input2.setBounds(1000, 450, 180, 30);
button.setBounds(1000, 520, 180, 30);
frame.getContentPane().add(button);
frame.getContentPane().add(inputLabel1);
frame.getContentPane().add(input1);
frame.getContentPane().add(input2);
frame.getContentPane().add(inputLabel2);
frame.getContentPane().add(inputLabel3);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
String sql = "SELECT id, userName, password, firstName, lastName FROM employee ;";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection) DriverManager.getConnection(
"Database", "user", "Password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
String user = input1.getText();
String pwd = (input2.getText());
while (rs.next()) {
String uname = rs.getString("userName");
//Username is the coloumn name in the database table
String password = rs.getString("password");
if ((user.equals(uname)) && (pwd.equals(password))) {
frame.dispose();
ScreenInfoFrame ui = new ScreenInfoFrame();
ui.setVisible(true);
}
}
} catch (ClassNotFoundException | SQLException k) {
JOptionPane.showMessageDialog(null, k.getMessage());
}
}
});
}
}