Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8023189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:29:54+00:00 2026-06-04T22:29:54+00:00

Basically, what I wanted to do is to close a JFrame with a button

  • 0

Basically, what I wanted to do is to close a JFrame with a button in the same class. I have 2 classes Class1 and Class2. When I click on the button “Add data” in Class1, it will open Class2 (sort of like a dialog box) and I want to close Class2 when I click on the “Done” button.

——————————-Class1——————————-

public class Class1 extends JFrame{

private JPanel contentPane;

      public Class1(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 355, 251);
        contentPane = new JPanel();
        contentPane.setBackground(Color.WHITE);
        contentPane.setForeground(Color.BLACK);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

//Add labels and other relevant content here

        JButton addData = new JButton("Add Data");
        addData.setBounds(32, 135, 130, 23);
        contentPane.add(addData);
        addData.addActionListener(new addDataActionListener());
      }
class addDataActionListener implements ActionListener{

        public void actionPerformed(ActionEvent arg5) {
            Class2 co = new Class2();
            co.setVisible(true);
        //opening the Class2 JFrame
        }

    }

}

——————————-Class2——————————-

public class Class2 extends JFrame {

    private JPanel contentPane;

      public Class2(){
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        setBounds(100, 100, 415, 238);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

//Add labels and other relevant content here

        JButton done = new JButton("Done");
        done.setBounds(206, 164, 89, 23);
        contentPane.add(done);
        done.addActionListener(new doneActionListener());
      }

    class doneActionListener implements ActionListener{
        public void actionPerformed(ActionEvent e) {

        //add stuff that needs to be done

            Class2 co2 = new Class2();
            co2.setVisible(false);
            co2.dispose();
}

However, when I click on the “Done” button, it performs all the other actions, but does not close the frame in Class2.
It would be appreciated if somebody can let me know how it’s done.

Ps. I’m new to Java (started about 4 months ago). Sorry if I’m not being clear enough. Thanks in advance 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T22:29:56+00:00Added an answer on June 4, 2026 at 10:29 pm

    Your doneActionListener‘s is doing wrong thing.

    Your code is actually creating new Class2 instance and close it, instead of close the current open one.

    Try this:

    class doneActionListener implements ActionListener{
    
        private JFrame toBeClose;
    
        public doneActionListener(JFrame toBeClose) {
            this.toBeClose = toBeClose;
        }
    
        public void actionPerformed(ActionEvent e) {
            toBeClose.setVisible(false);
            toBeClose.dispose();
        }
    }
    

    and

    public Class2(){   
        // other code 
        // pass this to doneActionListener to be close
        done.addActionListener(new doneActionListener(this));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to add a new property to one of my model (table). Basically
Basically I wanted to have an if-else statement in my linq to sql statement.
In C++, I have an Object class, from which a number of other classes
I wanted to have a URL like this www.mydomain.com/19127axbj1 and basically when this is
I wanted to configure a script that would basically take the opening Outlook 2010
If I wanted to create a flickable looping page menu basically like the Symbian
I wanted to create a new property on a table in my model.. Basically
Basically I'm making a filter for my portfolio so that you can click on
Basically my question is, if I have a number string, and i'm going to
Basically I have some simple code that does some things for files and I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.