Results 1 to 2 of 2
  1. #1
    noneofthem is offline Member
    Join Date
    Feb 2011
    Location
    Germany
    Posts
    7
    Rep Power
    0

    Question Need help with setDefaultCloseOperation

    Hello everybody,

    this might be a simple thing but I cannot find the solution anywhere. Maybe I need more coffee.


    Currently I have this

    Java Code:
    this.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
    and I want to make the "x" call this code when pressed

    Java Code:
    if ( e.getActionCommand().equalsIgnoreCase ( "exit" ) ) {
    			
    		int exitQuestion = JOptionPane.showConfirmDialog (
    					
    			null,
    			"Are you sure you want to exit now?",
    			"Done already?",
    			JOptionPane.OK_CANCEL_OPTION
    		);
    			
    		if ( exitQuestion == JOptionPane.YES_OPTION )
    				
    			System.exit ( 0 );
    			
    		else
    			return;
    }
    How do I accomplish that? Could anyone give me a tip, please? I am stuck here.

    Thanks a lot in advance!


    noneofthem
    - Lubuntu 10.10, Eclipse 3.5.2, Python 2.6.6, Java 6 -

  2. #2
    noneofthem is offline Member
    Join Date
    Feb 2011
    Location
    Germany
    Posts
    7
    Rep Power
    0

    Default

    I found the solution myself. Here is what I did, just in case someone else has this problem...

    1. I changed the first line of code to this:

    Java Code:
    this.setDefaultCloseOperation ( JFrame.DO_NOTHING_ON_CLOSE );
    2. I added a windowListener and set it up to call an exit method:

    Java Code:
    addWindowListener ( new WindowAdapter() { 
    	    
        public void windowClosing ( WindowEvent e ) { 
            exit();
        } 
    });
    3. I wrote an exit method to reflect the behaviour I wanted to achieve:

    Java Code:
    void exit () {
    		
        int exitQuestion = JOptionPane.showConfirmDialog (
    				
            null,
    	"Are you sure you want to exit now?\n" +
    	"Unsaved changes will be lost!",
    	"Done already?",
    	JOptionPane.OK_CANCEL_OPTION
        );
    		
        if ( exitQuestion == JOptionPane.YES_OPTION )
    			
    	System.exit ( 0 );
    		
        else
    	return;
    }
    Now everything is working as expected. I hope this will help someone.

    Cheers

    none of them
    - Lubuntu 10.10, Eclipse 3.5.2, Python 2.6.6, Java 6 -

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •