Hello All,

I have a Java program which is using a JOptionPane in it.

eg. Are you sure u want to close the application?
Yes              No

When I click with the mouse on the No button it works correctly but if I select the No button using the TAB key and press enter key the application still closes.

Can anyone tell me whats going wrong

    public class WindowHandler extends WindowAdapter
{
    public void windowClosing(WindowEvent e)
    {
        int OptionChoosed=JOptionPane.showConfirmDialog(null, "<html><p><font color=\"#FF8C00\" " +
                "size=\"4\" face=\"Book Antiqua\">Close the Application ?"+
                "</font></p></html>" ,"Warning",JOptionPane.YES_NO_OPTION);    
        System.out.println(OptionChoosed);
        if (OptionChoosed==0)
        {
            try
            {
                if (databaseInformationModel != null)
                {       // may have failed to be initialized for some reason
                    JOptionPane.showMessageDialog(null,"ABCD" +OptionChoosed,"null",JOptionPane.WARNING_MESSAGE); //just a debugger
                    databaseInformationModel.close();   // we want to shut it down and compact it before exiting
                    System.exit(0);
                }
            }
            catch (Exception e1){e1.printStackTrace();}
        }
        else if (OptionChoosed==1)
        {
            System.out.println(OptionChoosed+"Do nothing");
        }
        JOptionPane.showMessageDialog(null,"OUT" +OptionChoosed,"null",JOptionPane.WARNING_MESSAGE); //just a debugger

    }//windowClosing
}//WindowHandler

I need the button to react to the mouse listener as well as the key listener depending on the users choice

What is printed when the code is executed
System.out.println(OptionChoosed);

What are the values 0 and 1 that you are comparing against? The code should use the variables defined in the JOptionPane class vs hardcoding numbers?

JoptionPane returns the int values 0 or 1....
yes means 0 and no means 1....
if I select the No button with the Tab key and press Enter key it returns 0 instead of 1
and if I click on the No button with the help of mouse it gives the correct value that means 1.

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.