Results 1 to 3 of 3
  1. #1
    izzy is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default JOptionPane - editing options

    I have a JOption Pane with customised options text, I was just wondering if there's a way to make the options do something else other than just close the window when it pops up?
    This is what i have so far:

    Java Code:
    Object[] options = {"Play Again", "Exit"};
    
    JOptionPane.showOptionDialog (frame, "Player X wins", "Congratulations", 
    JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options,
     options[0]);
    I already have defined methods in this class to play again and exit, and i want them to be called when the user selects one of these options on this JOptionPane. But at the moment, whatever button you click, it just closes the window.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If you want the dialog to remain open, you'll need to use a custom JDialog. Otherwise if you want to play again or exit based on the response of the user, simply get the button pressed after the option pane closes.

    Java Code:
    int response = JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options,
     options[0]);
    if (response != JOptionPane.CLOSED_OPTION) {
      if (options[response].equals("Play Again")) {
         // do your stuff to play again
      } else {
        // exit
      }
    }
    note 1: code not tested.
    note 2: please do not multipost the same question in multiple fora on this site. Also don't cross-post on other sites without notifying others in your posts. Thanks for your cooperation.
    note 3: you may wish to reply to the posts made in your thread from yesterday. Your thanks are how folks are "paid" around here. Again, thanks for your cooperation.

  3. #3
    izzy is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    thanks, this now works well. thanks for your help.
    Last edited by izzy; 03-22-2010 at 05:26 PM. Reason: problem now sorted

Similar Threads

  1. VM options + applet
    By ProgrammingPup in forum Java Applets
    Replies: 1
    Last Post: 12-24-2009, 05:21 AM
  2. Menu Options For User
    By mnand in forum New To Java
    Replies: 1
    Last Post: 02-02-2009, 03:12 AM
  3. JOptionPane - showConfirmDialog(...) options
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-17-2007, 09:38 AM
  4. ComboBox with database options
    By Goldy in forum Advanced Java
    Replies: 0
    Last Post: 12-01-2007, 09:43 PM
  5. problem with Java Vm options
    By DonnieDarko in forum Advanced Java
    Replies: 1
    Last Post: 11-21-2007, 03: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
  •