• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Help me with swing event on close

 
Ranch Hand
Posts: 102
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello! I made a programm with NetBeans GUI Builder. When I use the programm, I write some infos and then save, after that I close the programm and it must check if all is saved.
So I save the data in serialized ArrayList and after that clear this ArrayList. My close window is in constructor like in one YouTube video... Now I do not understand, where I have to put this check when click close button?
Here constructor:
 
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Savvy wrote:. . . I write some infos and then save, after that I close the programm and it must check if all is saved. . . . serialized ArrayList and after that clear this ArrayList.

What happens if your default close operation is DO_NOTHING_etc? Does the frame close? Is an event fired and does the listener respond to it? Beware: the default is HIDE_ON_CLOSE, so you can have your frame disappearing even if you don't decide to close it.
Why are you clearing the List? Do you have some way of recording whether the List has changed since it was last stored anywhere? Do you have a save button anywhere? You can call the save button, possibly with this method, or some other way. Or, better, you have a save() method which you call directly or indirectly from the windowClosing() method.

My close window is in constructor like in one YouTube video...

Which video? I worry whenever I hear somebody say they found it on the Net, because there are some good tutorials out there and some bad ones. In my experience, the bad ones outnumber the good ones.

. . . .

That is a poorly‑chosen name for a class; I hope it was chosen for you, in which case yoiu can blame somebody else
I hope there isn't any sign of class A09 extends JFrame anywhere; that would be even worse.

That worries me; I would prefer to see myFrame.addWindowListener(...);, for the same reason as I wouldn't want to see extends. A GUI app HAS‑A JFrame; it usually IS‑NOT‑A JFrame.

I think this would be better code:-You don't need the local variable x. I woould only use null for the first argument if you are using a command‑line app and want the dialogue to appear in the centre of the screen. Make sure to check the options for the last argument. Put the closing code and a call to save() in their own method.
 
You will always be treated with dignity. Now, strip naked, get on the probulator and hold this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic