2

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 :)

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
savidude
  • 73
  • 2
  • 11

5 Answers5

3

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));
}
Pau Kiat Wee
  • 9,485
  • 42
  • 40
2

I did not understand the need for co2 in the doneActionListener. I believe you could solve this problem by changing the final lines to:

   this.setVisible(false);
   this.dispose();

I did not test it, though. Please advise me if the solution works.

rlinden
  • 2,053
  • 1
  • 12
  • 13
2
  • don't create more than one JFrame, if you need popup window look at JDialog with parent to JFrame

  • don't extends JFrame, you have to create local variable, otherwise you can't lost reference for this Object in the JComponents hierachry

  • I'd use CardLayout instead, tutorial about CardLayout

  • for hide JFrame you have to JFrame#setDefaultCloseOperation(HIDE_ON_CLOSE) or call from JButtons ActionListener code JFrame#setVisible(false)

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    I don't know much about Java, since I'm new and I have to attend to school work and stuff. I understand what you meant. I'll try to do this again without using more than one JFrame. Thanks :) – savidude May 31 '12 at 11:39
  • *"I'll try to do this again without using more than one JFrame."* It will save a lot of head-aches, down the track. – Andrew Thompson May 31 '12 at 11:46
0

*Try these changes in doneActionListener*

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

        //add stuff that needs to be done

            SetVisible(false); //since its already extending the JFrame

    }

or simply use ....System.exit(0);

Kabilan S
  • 1,104
  • 4
  • 15
  • 31
0

I was actually searching for the same answer and found that in the ActionListener corresponding to your JButton you should insert

this.setVisible(false);

this.dispose();

To just terminate and hide the current JFrame and the parent JFrame will resume as such while for the whole program to terminate include

System.exit(0);

and this will make all the JFrame's dissapear

talegna
  • 2,407
  • 2
  • 19
  • 22