Skip to content
Advertisement

Close window after checking

I’m having a slight issue, and I can’t figure it out. I want for my code to check if the email and password matches and then close the window. This the action:

btnLogin.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {

        boolean status = Email_Verification.email_validation(email_text.getText().toString().trim());
        if (status) {
            lbl_inco_email.setText(null);
        } else {
            lbl_inco_email.setText("Please enter a valid email");
        }

        boolean stat = Password_Verification.password_validation(password_Field.getPassword().toString());
        if (stat) {
            lbl_inco_pwd.setText(null);
        }   else {
            lbl_inco_pwd.setText("Please enter a valid Password");
        }

        /*      Exit and redirect to the main application if the email/pwd matches the database */

        /** MAIN__WINDOW __EXIT__ONCLICK__ = new MAIN__WINDOW();
        __EXIT__ONCLICK__.setVisible(true); */
    }
});

Advertisement

Answer

I am going to assume you have an issue with opening a new frame and disposing of the login frame, because if you have an issue with user validation there isn’t enough information for any of us to actually help.

Also, please read this concerning the use of multiple JFrames. The Use of Multiple JFrames: Good or Bad Practice?

Now for the code section…

Once user validation is done, you need to create an instance of your main window and make it visible. After that you can close the first JFrame.

Your actionPerformed would look something like this:

btnLogin.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
       // Let's assume you have a boolean variable that handles user validation 
          //for simplicity...
       if(userVerified) {
         // In this example let's call the main application window MainFrame
          MainFrame mF = new MainFrame();
          // Set it to visible
          mF.setVisible(true);
          mF.setLocationRelativeTo(null);
          // Now you can close the first JFrame
          this.dispose();
       }
    }
});
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement