Results 1 to 3 of 3
  1. #1
    hego555 is offline Member
    Join Date
    Jun 2011
    Posts
    4
    Rep Power
    0

    Default JPanel Options Help

    Okay So I am updating my Rock Paper Scissors game, and I want to make it so you choice one of these 4 buttons

    Rock, Paper, Scissors, Other Command

    so this is my code, can you please help me fix it!
    Thanks

    (I used code from Oracle and someone who tried helping on a diffrent site, not all mine but I did modify)

    Java Code:
    Object[] options = {"Rock",
                        "Paper",
                        "Scissors",
                        "Other Command"};
    int n = JOptionPane.showOptionDialog(null,
        "",
        "",
        JOptionPane.YES_NO_CANCEL_OPTION,
        JOptionPane.QUESTION_MESSAGE,
        null,
        options,
    	options[3]);
    
    	if (n == javax.swing.JOptionPane.YES_OPTION) {
    	  JOptionPane.showMessageDialog(null, "WORKS FOR ROCK",
    "Win", 1);
    	}
    	else if (n == javax.swing.JOptionPane.NO_OPTION) {
    	  JOptionPane.showMessageDialog(null, "WORKS FOR Scissors",
    "Win", 1);
    }
    else if (n == javax.swing.JOptionPane.CANCEL_OPTION) {
    	  JOptionPane.showMessageDialog(null, "WORKS FOR Paper",
    "Win", 1);
    }
    else if (n == javax.swing.JOptionPane.DEFAULT_OPTION) {
    	  JOptionPane.showMessageDialog(null, "WORKS FOR OTHER",
    "Win", 1);
    }
    Thanks, I hope for a fast responce as it is late and i need to sleep!

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Why do you make the dialog of type JOptionPane.YES_NO_CANCEL_OPTION since this is for questions that have yes/no/cancel type responses. Like "Do you want to save the file with the default file extension?" where the user can choose between "yes" (use the default extension), "no" (use the filename as given) or "cancel" (eg clicking the x of the dialog == abort the save operation). Your options are clerarly not of this type. You are providing the user with 4 options and they must choose one (or possibly cancel the choice if you allow this).

    Read through the showOptionDialog() API docs and choose a more appropriate option type. There aren't a lot to choose from.

    Remember that you can always see what the method is returning by inserting a little debugging code.

    Java Code:
    int n = JOptionPane.showOptionDialog(
            null, "", "",
            JOptionPane.???, JOptionPane.QUESTION_MESSAGE,  // buggar!
            null, options, options[3]);
    System.out.println("Answer was n=" + n);
    ----------

    Once you've seen what the return code is (and decided what you will do if n == javax.swing.JOptionPane.CANCEL_OPTION is returned because the user clicks the x or taps the escape key) then remember that you can use a switch statement rather than the if/else-if/else-if/.../else construction.

  3. #3
    hego555 is offline Member
    Join Date
    Jun 2011
    Posts
    4
    Rep Power
    0

    Default

    No i got what I was doing wrong, in the If statement!
    Thanks anyway!

Similar Threads

  1. JOptionPane - editing options
    By izzy in forum AWT / Swing
    Replies: 2
    Last Post: 03-22-2010, 06:05 PM
  2. VM options + applet
    By ProgrammingPup in forum Java Applets
    Replies: 1
    Last Post: 12-24-2009, 06:21 AM
  3. JOptionPane - showConfirmDialog(...) options
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-17-2007, 10:38 AM
  4. ComboBox with database options
    By Goldy in forum Advanced Java
    Replies: 0
    Last Post: 12-01-2007, 10:43 PM
  5. problem with Java Vm options
    By DonnieDarko in forum Advanced Java
    Replies: 1
    Last Post: 11-21-2007, 04:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •