Home > Software design >  Close JFrame and open another JFrame from another class
Close JFrame and open another JFrame from another class

Time:12-24

I am pretty new to Java. I have this particular issue that have kept me from moving to the next phase of my project. I want to close a JFrame from my main class from another class and open another jFrame on button click. I have been able to open the second JFrame on button click, but can't close the first JFrame. I am working with Netbeans.

NOTE: I'm trying to have separate codes for views and controller using an MVC design pattern.

Here are my codes.

LoginPage (Main class)

package auth;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class LoginPage extends javax.swing.JFrame {
    public static LoginPage lp = new LoginPage(); 
    
    public LoginPage() {
        initComponents();
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here: Cancel button
        System.exit(0);
    }                                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here: Login button
        String uname = jTextField1.getText();
        String pword = jPasswordField1.getText();
        
        try {
            LoginController.collectUserData(uname, pword);
        } catch (SQLException ex) {
            Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LoginPage().setVisible(true);
            }
        });
    }


and the other class

LoginController

package auth;

import dbconnect.dbconnect;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.*;

public class LoginController extends javax.swing.JFrame { 
    
    public static void collectUserData(String uname, String pword) throws SQLException {
        Login user = new Login();
        user.setUsername(uname);
        user.setPass(pword);
        checkDatabaseAndLogin(user);
    }

    public static void checkDatabaseAndLogin(Login test) throws SQLException {
        JFrame rootPane;
        if (test.getUsername().equals("")||test.getPass().equals("")) {
            rootPane = new JFrame();
            JOptionPane.showMessageDialog(rootPane, "Some fields are empty", "Error", 1);
        } else {
            try {
                //LoginPage lp = new LoginPage();
                Connection con = dbconnect.connect();
                PreparedStatement pst = con.prepareStatement("select * from test where username=? and pass=?");
                pst.setString(1, test.getUsername());
                pst.setString(2, test.getPass());
                ResultSet rs = pst.executeQuery();
                                
                if (rs.next()) {
                    String un = rs.getString("username");
                    //System.out.println(un);
                    PatronPage pt = new PatronPage(un);
                    pt.setVisible(true);  //Code to open the new window
                    LoginPage.lp.dispose(); //Code to close the old window
                } else {
                    rootPane = new JFrame();
                    JOptionPane.showMessageDialog(rootPane, "Username or Password do not match record", "Login error", 1);
                } 
            } catch (Exception ex) {
                System.out.println("" ex);
            }
        }
    }
    
}

Of course there are other system generated codes in Netbeans I removed, I just provided the chunk of code that I feel are relevant to the solution. Please help out, I'm stuck.

CodePudding user response:

The LoginPage instance that you are "closing" in your LoginController class with the line

LoginPage.lp.dispose();

is not the instance which you initially displayed with

new LoginPage().setVisible(true);

I am afraid your whole approach to creating a Swing UI is wrong. Maybe work through the Swing tutorial first.

  • Related