Dewsworld
Dewsworld

Reputation: 14033

Java making confirming exit

I have an application. I need to make this, when user click the close button, a confirmation will come ( just like javascript confirm dialog ) , 'Are you sure'? I'm using Swing visual editor in Netbeans and I'm using a listener on window closing. But I can't make it. Any help?

Upvotes: 2

Views: 3159

Answers (1)

shubhendu mahajan
shubhendu mahajan

Reputation: 816

Try this code:

jj4.addActionListener(new ActionListener()  // jj4-button's reference to implement exit
    {
        public void actionPerformed(ActionEvent ae)
        {
            if(!jtextarea.getText().equals("") && !jtextarea.getText().equals(fileContent))
            {
                if(fileName == null)
                {
                    //this method have 3 options (1 = YES, 2 = NO, 3 = Cancel)
                    option = JOptionPane.showConfirmDialog(null,"Do you want to save the changes ??");
                    if(option == 0)
                    {
                        //to save the text into file
                        saveAs();
                        System.exit(0);
                    }
                    if(option == 1)
                    {
                        //to exit the program without saving
                        System.exit(0);
                    }
                }
                else
                {
                    option = JOptionPane.showConfirmDialog(null,"Do you want to save the changes ??");
                    if(option == 0)
                    {
                        save();
                        System.exit(0);
                    }
                    if(option == 1)
                    {
                        System.exit(0);
                    }
                }
            }
            else
            {
                System.exit(0);
            }
        }
    });

Upvotes: 6

Related Questions