'JFrame not "disposing" on actionperformed method

Background: I am creating a login browser page before my main UI(myGUI) page is displayed. I am using a HashMap to store the correct username and password combinations. The key is put in a getter method where I then access it from the LoginBrowser class constructor, where I build the actual UI.

Problem: Currently, the key values are stored correctly and login is successful. It properly loads up the main UI after the correct credentials are entered. I tried to instantiate the class with the key inside and then call the dispose method. However, the login browser UI does not go away afterwards. What is wrong with my instantiation and how would I solve it?

My current code:

Main method:

    public static void main(String[] args) {
        
       IdAndPasswords s = new IdAndPasswords();        

       java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
               LoginBrowser lb = new LoginBrowser(s.getInfo());
                lb.setVisible(true);
                //myGUI.requestFocusInWindow(); // makes sure textfield or other components don't auto focus on start-up
                lb.setTitle("Chat App");
                lb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
        }); 
    }

Login Browser Class Contructor:

 HashMap<String,String> loginInfo = new HashMap<String,String>();

/**
 * Creates new form LoginBrowser
 * @param loginOG
 */
public LoginBrowser(HashMap<String,String> loginOG) {

    initComponents();
    this.loginInfo = loginOG;
 
}

Login Browser Action Performed method:

/*
If login button to perform actions
when pressed
*/
private void loginBtnActionPerformed(java.awt.event.ActionEvent evt) {                                         
    if(evt.getSource()==loginBtn) {
       String userIDNew = usernameText.getText(); // get text of JTextfield
       String passwordNew = String.valueOf(passwordText.getPassword()); // get text of JPasswordfield and convert
       
    if(loginInfo.containsKey(userIDNew)) { // key
         // if entered characters in strings match up, 
         // display message and get rid of login browser
        if(loginInfo.get(userIDNew).equals(passwordNew)) {
            JOptionPane.showMessageDialog(rootPane, "Login successful");
            IdAndPasswords s = new IdAndPasswords();         
            LoginBrowser lb = new LoginBrowser(loginInfo);
            lb.dispose(); // dispose login browser
    // Once old form is disposed, open main gui form
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            myGUI myGUI = new myGUI(userIDNew);
            myGUI.setVisible(true);
            //myGUI.requestFocusInWindow(); // makes sure textfield or other components don't auto focus on start-up
            myGUI.setTitle("Chat App");
            myGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    });
        // tell user if info entered is incorrect
        } else {
            JOptionPane.showMessageDialog(rootPane, "Incorrect Password");
           }
        } else {
            JOptionPane.showMessageDialog(rootPane, "Incorrect Username");
       }        
   }
} 

  


Solution 1:[1]

Instead of instantiating class with key:

  LoginBrowser lb = new LoginBrowser(loginInfo);
   lb.dispose(); // dispose login browser

Simply use this. to control JFrame from within design feature of netbeans:

this.dispose();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 schoggi_23