i need to how to give codings to buttons that are present in confirm dialog box. For Example if there are two buttons as "YES" and "NO" and if i click "YES" then the program should close. If i click no then th dialog box should return to the program. Given below are the codes that i have done up to now and i dont have any idea to proceed further. Please help.

class cancelListener implements ActionListener
{
       public void actionPerformed(ActionEvent ev)
       { 
                int n = JOptionPane.showConfirmDialog( frame,
                                        "Are You sure you want to Close?",
                                        "Confirm Log Off",
                                         JOptionPane.YES_NO_OPTION);
        }
}

Recommended Answers

All 5 Replies

You need to evaluate return value from JOptionPane stored in "n" and compare to possible outcomes so in your case only YES.

int n = JOptionPane.showConfirmDialog( frame,
    "Are You sure you want to Close?",
    "Confirm Log Off",
    JOptionPane.YES_NO_OPTION);
if(n == JOptionPane.YES_OPTION){
//CLOSE APPLICATION ACTION HERE
}

Ignore my post, peter_budo is correct :)

i need to how to give codings to buttons that are present in confirm dialog box. For Example if there are two buttons as "YES" and "NO" and if i click "YES" then the program should close. If i click no then th dialog box should return to the program. Given below are the codes that i have done up to now and i dont have any idea to proceed further. Please help.

class cancelListener implements ActionListener
{
       public void actionPerformed(ActionEvent ev)
       { 
                int n = JOptionPane.showConfirmDialog( frame,
                                        "Are You sure you want to Close?",
                                        "Confirm Log Off",
                                         JOptionPane.YES_NO_OPTION);
        }
}

thanx a lot bro!!! it really works.

You need to evaluate return value from JOptionPane stored in "n" and compare to possible outcomes so in your case only YES.

int n = JOptionPane.showConfirmDialog( frame,
    "Are You sure you want to Close?",
    "Confirm Log Off",
    JOptionPane.YES_NO_OPTION);
if(n == JOptionPane.YES_OPTION){
//CLOSE APPLICATION ACTION HERE
}

Thanx a lot bro!!!. It worked out and sorry for the mistake's, im new to JAVA as well as DANIWEB. thanx again for replying to my thread!!

Thanx a lot bro!!!. It worked out and sorry for the mistake's, im new to JAVA as well as DANIWEB. thanx again for replying to my thread!!

No need to apologies. 3 years back I was in similar position.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.