I am working on my first ever GUI program, using Java Swing. I have a login form class that reads in a username and password, then checks to see if there is a matching username/password combo in a local database. If there is, I want to be able to return the username from the JFrame object to my main class. This would only be able to occur after my the action fires for clicking my login button.
I don't know how to make this class to return after that specific time:
public class Login extends JFrame {
//declaration of components
private JTextField userText = new JTextField(12);
private JPasswordField passText = new JPasswordField(12);
//declaration of variables
String username;
String password;
//...
class loginAction extends AbstractAction{
loginAction(){
super("Login");
putValue(SHORT_DESCRIPTION, "Click to login");
}
public void actionPerformed(ActionEvent e){
userLogin();
}
}
public String userLogin(){
String sql = "SELECT * from Employees " +
"WHERE Username = ? AND Password = ?";
try(PreparedStatement ps = con.prepareStatement(sql)){
ps.setString(1, userText.getText());
ps.setString(2, new String(passText.getPassword()));
ResultSet rs = ps.executeQuery();
if(rs.next())
//RETURN USERNAME BACK TO MAIN CLASS...How?
else
JOptionPane.showMessageDialog(this, "Incorrect username or password.", "", JOptionPane.ERROR_MESSAGE);
}
catch (SQLException e) {
utils.Print.print(e.getMessage());
printSuppressed(e);
}
finally{
}
}
//...
Login(Connection connect){
///...
JButton login = new JButton(new loginAction())
//...
}