0

I have a frame (called from the main window) with button, on click it run a cycle, in console i see: 1 2 3 4 5

The frame is closed by setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);

If I close the frame, and reopen it again, when click on the button in the console displays

1 2 3 4 5 1 2 3 4 5

the output is duplicate and so will increase each time when I re-open the frame (up to a complete restart of the program).

Please help! How to close the frame (without closing the program) without duplicate. Thank!

user1786639
  • 163
  • 11
  • [e.g. one of them](http://stackoverflow.com/questions/6309407/remove-top-level-container-on-runtime) – mKorbel Sep 18 '14 at 09:29
  • please provide an [MCVE](http://stackoverflow.com/help/mcve) so we can tell you where your problem is. Good Luck – Frakcool Sep 18 '14 at 17:17

2 Answers2

0

Instead of using DISPOSE_ON_CLOSE, try EXIT_ON_CLOSE. Doing this should fix your problem...

deo toh
  • 15
  • 1
  • 7
  • OPs need to hear --> to reuse current `JFrame` by using `CardLayout` – mKorbel Sep 18 '14 at 09:34
  • EXIT_ON_CLOSE will close programm, and I need close only current frame – user1786639 Sep 18 '14 at 09:34
  • @user1786639 Top-Level Containers haven't finalize(), then never will be GC'ed, this reason is simple ---> are based on peers from Native OS, JVM is virtual machine (like rest of) and hasn't the access to this libraries (security, etc) – mKorbel Sep 18 '14 at 09:39
0
perhaps a windowlistener may help. 

JFrame.addWindowListener(new WindowAdapter(){
           @Override
           public void windowClosing(WindowEvent e){
                   - - - - - - - - insert your code here - - - - - - - 
                  - - - - - - whatever you want to happen before - - - - - 
                  - - - - - -your form closes - - - - - - 
                   JFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

           }
       });
deo toh
  • 15
  • 1
  • 7