3

Hi I was wondering if there is a way to set a jbutton to do the equivalent of setdefaultcloseoperation(JFrame.DISPOSE_ON_CLOSE) because I dont understand how a frame object is eligible for collection if it still has listeners when the frame is set invisible. I can send code if this doesn't make sense.

Ben Evans
  • 33
  • 3

2 Answers2

2
  • Top Level Containers (JFrame, JDialog ...) miss implemetations for finalize(), then they are never GC'd

  • equivalent for JFrame.DISPOSE_ON_CLOSE is only setVisible(false)

  • you can returns all Top Level Containers from method Window[] wins = Window.getWindows();

  • you can remove only all JComponents from ContentPane, for return used memory back

EDIT

  • only visible container with Focus can listening or firing events from Swing Listeners
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • +1, this info is always true, no one has the power to control GARBAGE COLLECTION :-), but wont' writing dispose(), will atleast make it available for the GC to collect it, though only JVM can tell as to when will that happen. – nIcE cOw Apr 23 '12 at 06:00
  • ya thats what i thought. so does setVisible(false) leave the frames listeners open? thus not making the frame eligible for gc? – Ben Evans Apr 23 '12 at 07:24
  • that make sense. just so i know i have understood your edit, is Focus what setVisible(bool) turns on and off? – Ben Evans Apr 23 '12 at 08:25
1
addActionListener( new ActionListener(){
            public void actionPerformed(ActionEvent e){
                   myframe.dispose();
                   }
            });
ApprenticeHacker
  • 21,351
  • 27
  • 103
  • 153