We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,166 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

JOptionPane returns wrong value when pressed using Tab key

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

2
Contributors
2
Replies
1 Day
Discussion Span
8 Months Ago
Last Updated
3
Views
poojavb
Posting Pro
524 posts since Nov 2011
Reputation Points: 39
Solved Threads: 77
Skill Endorsements: 5

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?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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.

poojavb
Posting Pro
524 posts since Nov 2011
Reputation Points: 39
Solved Threads: 77
Skill Endorsements: 5

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0701 seconds using 2.75MB