Easy java question

Discussion in 'OT Technology' started by Corp, Nov 14, 2006.

  1. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    I've been working on a java project for school that I'm almost done with. Theres only a couple of things i want to add to it, and one is making sure the user wants to close the program before saving when they press the x in the corner. I already have it so that it will pop up a confirmation box if the user goes to file - exit, but i haven't been able to figure out how to do it for if they press the x in the corner. I have a window listener set up, but i cant get it to work quite right.

    this is what i have for my WindowClosing method
    Code:
    	public void windowClosing(WindowEvent e)
    	{
    		//the accounts have not been changed since last save, just exit
    		if(!bank.getChanged())
    			System.exit(0);
    		//the accounts have been changed since the last save, ask if the user wants to save
    		int selection = JOptionPane.showConfirmDialog(null, "Do you want to save?");
    		//the user selects no, exit
    		if(selection == JOptionPane.NO_OPTION)
    			System.exit(0);
    		//the user selects yes, save and exit
    		if(selection == JOptionPane.YES_OPTION)
    		{
    			save();
    			System.exit(0);
    		}
    	}
    
    This works as in it pops up a window asking if they want to save, but if i press cancel or the x in the top of the pop up window it will still exit. I guess what my main question is, once the x is pressed, how do i get the program to stop from exiting if the user presses cancel?
    thanks for any help
     
  2. sprite

    sprite Active Member

    Joined:
    May 20, 2003
    Messages:
    2,967
    Likes Received:
    0
    Location:
    MI, USA
    I don't do much client side Java but...

    Somewhere on the Window or Frame or something there is a property called setcloseoperation or setdefaultcloseoperation or something like that and you want to set it do_nothing or similar. That way it won't exit unless you explicitly tell it to exit.
     
  3. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    :uh: i didn't even think of this. but when i set the defautlcloseoperation to do nothing, how can i get it to even ask? when the user presses the x it wont still call the windowClosing method will it? I'd try it out now, but i have alot of reading for another class to do, if nobody knows, I'll end up messing around with it some more either tonight or tomorrow.
    thanks
     
  4. sprite

    sprite Active Member

    Joined:
    May 20, 2003
    Messages:
    2,967
    Likes Received:
    0
    Location:
    MI, USA
    It still fires the windowClosing event... there is just no default action associated with it... it will DO_NOTHING unless you tell it otherwise.
     
  5. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    alright, thanks for the help :cool:
     

Share This Page