Reputation: 11
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())
//...
}
Upvotes: 1
Views: 488
Reputation: 347184
JPanel
. It gives you greater flexibility and re-use options in the long run.JDialog
instead of a JFrame
, or better yet, use a JOptionPane
was it will take care of the buttons for you (although it will need to close the dialog before you can perform you validation checks).Upvotes: 2
Reputation: 285405
Don't use a JFrame for this as this is what modal dialogs such as JDialogs and JOptionPanes were created for. This is their reason for existence, and they would be very sad if you didn't use them for this. Seriously though, you know what happens when you call a JOptionPane, how the code flow from the calling program stops and waits for the JOptionPane to return before proceeding, well that's what modal JDialogs do (and in fact a JOptionPane is just a variant of JDialog), and this is the very behavior that you desire.
Note that you should almost never have a class extend JFrame regardless of whether you're using one or not. Instead gear your code towards creating JPanels, then stuff them in JDialogs, JFrames or whatever container is necessary when you want to display them.
Upvotes: 4
Reputation: 13289
One way to do this is to replace the commented line with a line that sets a variable inside the action. You'll then need to keep a reference to the action.
e.g.,
loginAction la = new loginAction()
JButton login = new JButton(la)
....
// some other code block with reference to la
if(la.isUsernameValid())
la.getUsername()
As @Hovercraft has mentioned, though, there are better ways to do this
Upvotes: 1