Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: JOption Pane help

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default JOption Pane help

    Im creating a quiz game using the JOption panes for my computer programming 1 class but I cant figure out this one detail. This is what I have so far:

    import javax.swing.JOptionPane;
     
    public class FinalProject {
     
        public static void main(String[] args) {
        	JOptionPane.showMessageDialog(null, "Welcome to my Sports Quiz, on the next window, you will be prompted to continue.");
        	JOptionPane.showConfirmDialog(null, 
    			"Play?", "Sports Quiz", JOptionPane.YES_NO_OPTION);
    		//int YesNo = 1; 
    		Switch (YesNo); {
    		                case yes : JOptionPane.YES_OPTION: System.out.print("Good"); break;
        		                case no : JOptionPane.NO_OPTION: System.exit(0); break;
    		                default: JOptionPane.showMessageDialog(null, "Invalid Response"); break;
    		}
     
        }
     
     
    }


    I want the Yes in the Confirm Dialog to continue to the next JOption Pane, and if they click the no I want it to exit. Once I figure out how to get the confirm dialog to work I can finish the rest easily. I just can't figure this out, can anyone help me?
    Last edited by dallas1125; November 17th, 2009 at 06:30 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: JOption Pane help

    You need to get the return value from the showConfirmDialog method:

    int answer = JOptionPane.showConfirmDialog(null,"Play?","Sports Quiz",JOptionPane.YES_NO_OPTION);
     
    Switch(answer)
    {
         case JOptionPane.YES_OPTION:
              // yes stuff
         case JOptionPan.NO_OPTION:
              // no stuff
         default:
              // default stuff
    }

  3. The Following 2 Users Say Thank You to helloworld922 For This Useful Post:

    chronoz13 (November 18th, 2009), dallas1125 (November 17th, 2009)

  4. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JOption Pane help

    Thanks, so it should look like this then? Thanks, I asked my teacher for help but she didn't know what to do. Ill be around here some and try to help out but im not that great at this. Thanks again!

    import javax.swing.JOptionPane;
     
    public class FinalProject {
     
        public static void main(String[] args) {
        	JOptionPane.showMessageDialog(null, "Welcome to my Sports Quiz, on the next window, you will be prompted to continue.");
        	JOptionPane.showConfirmDialog(null, 
    			"Play?", "Sports Quiz", JOptionPane.YES_NO_OPTION);
    int answer = JOptionPane.showConfirmDialog(null,"Play?","Sports Quiz",JOptionPane.YES_NO_OPTION);
     
    Switch(answer)
    {
         case JOptionPane.YES_OPTION:
              // yes stuff
         case JOptionPan.NO_OPTION:
              // no stuff
         default:
              // default stuff
    }
    }
    }
    Last edited by dallas1125; November 17th, 2009 at 11:40 PM.

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: JOption Pane help

    That'll show the confirm dialog twice. Also, I got lazy and put the comments in for the switch statement. You'll need actual code in there if you want it to do anything useful.

    import javax.swing.JOptionPane;
     
    public class FinalProject {
     
        public static void main(String[] args) {
        	JOptionPane.showMessageDialog(null, "Welcome to my Sports Quiz, on the next window, you will be prompted to continue.");
    int answer = JOptionPane.showConfirmDialog(null,"Play?","Sports Quiz",JOptionPane.YES_NO_OPTION);
     
    Switch(answer)
    {
         case JOptionPane.YES_OPTION:
              // yes stuff
         case JOptionPan.NO_OPTION:
              // no stuff
         default:
              // default stuff
    }
    }
    }

  6. The Following 2 Users Say Thank You to helloworld922 For This Useful Post:

    chronoz13 (November 18th, 2009), dallas1125 (November 18th, 2009)

  7. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: JOption Pane help

    ahh so thats how to control the two buttons of the showConfirmDialog method.... tnx for that world!

  8. #6
    Junior Member
    Join Date
    Nov 2009
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JOption Pane help

    Ok, thanks that solved it!