Search code examples
javaswingdispose

I cannot close JFrame


I have two java forms: NewJFrame i NewJFrame1. I have button on NewJFrame, so when I click on that button to open NewJFrame1 and close NewJFrame. It can open NewJFrame1, but it cannot close NewJFrame. This: NewJFrame frame = new NewJframe(); frame.setVisible(false); doesn't work. Also, frame.dispose(); doesnt work. CAn someone help me to soleve problem, how can I close NewJFrame by clicking on button in it (NewJFrame).


Solution

  • In your code

    NewJFrame frame = new NewJFrame();
    

    creates a new (second) instance of NewJFrame. If you want to close the original one, you need a reference to this instance. Depending on your code, the reference could be this, so

    this.dispose();
    

    could work.