Amaan Miah
Amaan Miah

Reputation: 1

The method dispose () is undefined for the type new ActionListener(){}

I am trying to make it so that my jframe is closed once the user presses login. However, it does not let me dispose the login window. Please help me. thank you.

JButton btnLogin = new JButton("Login");
    btnLogin.setFont(new Font("Tahoma", Font.PLAIN, 12));
    btnLogin.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
    
             
            
            String password = txtPassword.getText();
            String username = txtUsername.getText();
            
            if (password.contains("Marvel")&& username.contains("KingSlayer")) {
                txtPassword.setText(null);
                txtUsername.setText(null);
                
                {
                    
                 Main_menu info = new Main_menu();
                 
                 info.setVisible(true);
                 dispose();
                 
                }

Upvotes: 0

Views: 554

Answers (1)

camickr
camickr

Reputation: 324157

I like to use generic code so I don't need to know the name of any of the classes involved.

In the ActionListener you can use code like:

JButton button = (JButton)e.getSource();
SwingUtilities.windowForComponent(button).dispose();

The above code will get a reference to the window containing the button that was clicked so you can invoke the dispose() method.

Upvotes: 1

Related Questions