Results 1 to 8 of 8
  1. #1
    kris5228 is offline Member
    Join Date
    Nov 2011
    Posts
    17
    Rep Power
    0

    Default JOptionPane.showOptionDialog

    Hello,

    I've made it so that when i click the "x" (exit the window) it shows and JOptionPane.showOptionDialog that prompts if i want to save.

    The problem here now is that i have 3 options. "Yes", "No" and "Cancel". I want it so that when i click the Cancel button it returns to the frame, instead of exiting it.

    Java Code:
    public class Exit
    {
        private static Main mainfile = new Main();
        private static MenuBar menubar = new MenuBar();
    
        public void onexit()
        {
            if (mainfile.fn.equals("unnamed"))
            {
                Object[] options = { "YES", "NO", "CANCEL"};
    
                int response = JOptionPane.showOptionDialog(null, "Do you want to save " + mainfile.fn + "?", "Warning",
                JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
    
                switch (response)
                {
                    case 0:
                        menubar.saveas();
                    case 1:
                    case -1:
                        System.exit(0);
                    case 2:
    
                }
            }
            else
            {
                mainfile.frame.dispose();
            }
        }
    }
    Here the WindowListener is called:
    Java Code:
            frame.addWindowListener(new WindowAdapter()
            {
                public void windowClosing(WindowEvent e)
                {
                    OnExit.onexit();
                }
            });
    And how is it possible to do a mainfile.fn != "unnamed" ? Since the != isn't correct, is there any code for that? Or do i have to rearrange the code?
    Last edited by kris5228; 11-26-2011 at 08:57 PM.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: JOptionPane.showOptionDialog

    Don't use "magic number". The case values of 0, 1, -1, 2 mean nothing to me. Use the field names provided in the JOptionPane API to test which button was clicked.

    In your previous posting you are using:

    Java Code:
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Which tells the frame to exit the JVM. Instead you should be using:

    Java Code:
    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    now you are responsible for closing the frame.

  3. #3
    kris5228 is offline Member
    Join Date
    Nov 2011
    Posts
    17
    Rep Power
    0

    Default Re: JOptionPane.showOptionDialog

    Thank you.

    I still don't understand what you mean by using the "Field Names" provided?

    The 0, 1, -1, 2 works fine for me

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: JOptionPane.showOptionDialog

    In your code why did you use:

    JOptionPane.DEFAULT_OPTION and JOptionPane.WARNING_MESSAGE

    Why did you not use number2 like 1,2,3 4, or whatever? You used those variable names because they are defined as part of the API to make the class easier to use and understand.

    All the defined variable names are documented as part of the API in the "Field Summary" section of the API. You should be using the appropriate variable names that represent the Yes, No and Cancel buttons. The numbers 0, 1, 2 make no sense to anybody reading the code.

  5. #5
    kris5228 is offline Member
    Join Date
    Nov 2011
    Posts
    17
    Rep Power
    0

    Default Re: JOptionPane.showOptionDialog

    How would you then write the code?

    Could you maybe make an example?

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: JOptionPane.showOptionDialog

    Replace the 0, 1, 2, with the appropriate variable name found in the "Field Summary".

  7. #7
    kris5228 is offline Member
    Join Date
    Nov 2011
    Posts
    17
    Rep Power
    0

    Default Re: JOptionPane.showOptionDialog

    What?

    I have no int variable(which it requires) so what should i then use?

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: JOptionPane.showOptionDialog

    Did you read the API???

    It has the variable names defined for you.

    Where do you think the variable names JOptionPane.DEFAULT_OPTION and JOptionPane.WARNING_MESSAGE came from???

Similar Threads

  1. Replies: 3
    Last Post: 07-28-2011, 07:52 AM
  2. Help with closing JOptionPane.showOptionDialog
    By Rafik in forum New To Java
    Replies: 3
    Last Post: 06-07-2011, 06:42 AM
  3. Help with JOptionPane
    By javaman1 in forum New To Java
    Replies: 6
    Last Post: 11-13-2010, 10:40 PM
  4. Help with JOptionPane
    By SwEeTAcTioN in forum New To Java
    Replies: 12
    Last Post: 11-03-2009, 04:12 AM
  5. Replies: 0
    Last Post: 12-17-2007, 09:46 AM

Posting Permissions

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