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);    
        if (OptionChoosed==0)
                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
            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


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

Edited by poojavb

4 Years
Discussion Span
Last Post by poojavb

What is printed when the code is executed

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?

Edited by NormR1


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.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.