'Can't close JFrame, from click inside JPanel

I have the following situation:

I have a JFrame(GridBagLayout) with 3 JPanels in it (headerPanel, bodyPanel, footerPanel); On the bodyPanel I have a login button; What I want, is to close the JFrame, an open another one;

Main:

package com.system.reservation.airline;

public class Main {
    public static void main(String[] args) { // Main function that executes the program
        LoginPage loginPage = new LoginPage();
    }
}

LoginPage:

public class LoginPage extends JFrame{
    HeaderPanel headerPanel = new HeaderPanel();
    BodyPanel bodyPanel = new BodyPanel();
    FooterPanel footerPanel = new FooterPanel();

    public LoginPage(){ // Constructor
        this.setTitle("Login"); // Frame title
        this.setSize(400, 600); // Frame size
        this.setLocationRelativeTo(null); // Center frame, when launched
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Frame function, on close button (will close)
        this.getContentPane().setBackground(new Color(240,248,255)); // Light blue
        this.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();

        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.fill = GridBagConstraints.BOTH;

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridheight = 1;
        this.add(headerPanel, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridheight = 1;
        this.add(bodyPanel, gbc);

        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridheight = 3;
        this.add(footerPanel, gbc);

        this.pack();
        this.setVisible(true); // Frame visible
    }
}

BodyPanel:

package com.system.reservation.airline.panels;

import com.system.reservation.airline.HomePage;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class BodyPanel extends JPanel implements ActionListener { // Listens to events

    JButton login_button;
    JTextField userName_txb, password_txb;
    JLabel tokenResponse;

    String username;
    String password;

    public void setUsername(String newUsername){
        username = newUsername;
    }
    public String getUsername(){
        return username;
    }

    public void setPassword(String newPassword){
        password = newPassword;
    }
    public String getPassword(){
        return password;
    }
    public BodyPanel(){
        Color transparent = new Color(1f,0f,0f,0f);

        this.setBackground(transparent);
        this.setLayout(new BorderLayout());
        //this.setBorder(BorderFactory.createLineBorder(Color.green, 3));
        //------------ Login Panel ------------------------
        JPanel login_panel = new JPanel(new BorderLayout());
        login_panel.setBackground(transparent);
        login_panel.setPreferredSize(new Dimension(400, 50));
        //login_panel.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 5));

        JLabel login_label = new JLabel();
        login_label.setText("Login");
        login_label.setHorizontalAlignment(JLabel.CENTER);
        login_label.setFont(new Font("Arial", Font.PLAIN, 20));

        login_panel.add(login_label, BorderLayout.CENTER);
        //------------ Login Panel ------------------------

        //------------ Input Panel ------------------------
        JPanel input_fields_panel = new JPanel(new GridBagLayout());
        input_fields_panel.setBackground(transparent);
        //input_fields_panel.setPreferredSize(new Dimension(400, 150));
        //input_fields_panel.setBorder(BorderFactory.createLineBorder(Color.black, 5));

        GridBagConstraints gbc = new GridBagConstraints();

        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.fill = GridBagConstraints.BOTH;

        //-------------
        JLabel userName_label = new JLabel("Username: ");
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.insets = new Insets(0,10,0,0);
        input_fields_panel.add(userName_label, gbc);

        userName_txb = new JTextField();
        userName_txb.setPreferredSize(new Dimension(250,30));
        gbc.gridx = 3;
        gbc.insets = new Insets(5,0,5,20);
        input_fields_panel.add(userName_txb, gbc);
        //-------------

        //-------------
        JLabel password_label = new JLabel("Password: ");
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.insets = new Insets(0,10,0,0);
        input_fields_panel.add(password_label, gbc);

        password_txb = new JTextField();
        password_txb.setPreferredSize(new Dimension(250,30));
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 3;
        gbc.gridheight = 1;
        gbc.insets = new Insets(5,0,5,20);
        input_fields_panel.add(password_txb, gbc);
        //-------------

        //------ Login Button -----------
        login_button = new JButton("Login");
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 4;
        gbc.gridheight = 1;
        gbc.insets = new Insets(10,150,5,150);
        login_button.addActionListener(this);
        input_fields_panel.add(login_button, gbc);
        //------ Login Button -----------

        tokenResponse = new JLabel("");
        tokenResponse.setHorizontalAlignment(JLabel.CENTER);
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridwidth = 4;
        gbc.gridheight = 1;
        gbc.insets = new Insets(0,0,0,0);
        input_fields_panel.add(tokenResponse, gbc);
        //------------ Input Panel ------------------------

        //-------------------------------------
        this.add(login_panel, BorderLayout.NORTH);
        this.add(input_fields_panel, BorderLayout.CENTER);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == login_button) {
            System.out.println("Olá");
            //-------------------------
            if(userName_txb.getText().replaceAll("\\s+","").equals("") || password_txb.getText().replaceAll("\\s+","").equals("")){ // removes white spaces
                // If either of the text fields are empty, a label is displayed
                System.out.println("It's empty!");
                tokenResponse.setText("The input fields are empty!");
            } else {
                String userName_value = userName_txb.getText();
                String userPassword_value = password_txb.getText();
                // User inputs are stored

                setUsername(userName_value);
                setPassword(userPassword_value);
                // User inputs are initialized
                //-----------------------
                try{
                    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/user_login", "root", "***********");
                    // Connect to the database;
                    Statement statement = connection.createStatement();
                    // It will initiate a command;
                    ResultSet resultSet = statement.executeQuery("select customer_username, customer_password from user_authentication");
                    // Execute the command;                                                 // column                   // table

                    while (resultSet.next()){ // While there are results, it will print them;
                        String name = resultSet.getString("customer_username");
                        String password = resultSet.getString("customer_password");

                        if(!getUsername().equals(name) || !getPassword().equals(password)){
                            tokenResponse.setText("The username or the password are incorrect!");
                            System.out.println("It does not match!");
                            //break;
                        } else {
                            // CLOSE JFRAME
                            System.out.println("It matches!");
                            HomePage homePage = new HomePage();
                            break;
                        }
                    }
                }catch(Exception e2){
                    e2.printStackTrace();
                }
            }
        }
    }
}

After I click login the JFrame on the left shows up. I want to close the one on the right.

enter image description here

Any help would be appreciated, thanks.



Solution 1:[1]

Answer from @camickr, managed to close Jframe, from click inside Jpanel:

login_button = (JButton) e.getSource();
SwingUtilities.windowForComponent(login_button).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 OrlandoVSilva