Search code examples
javaswingjoptionpane

How to make the JOptionsPane (Without a panel) close the program with X


So I know that there are a lot of similar questions asked on this site and through the internet in general, but I have not been able to find the exact answer or one that satisfies what I want to accomplish.

I have multiple JOptionPanes within my program. All of these JOptionsPanes have a "X" in the top corner. Currently they function just the same as any other JOptionPane's default button.

I want the program to exit if the user clicks the "X". With any other the other buttons the screen will close but with "X" I want to cause a System.exit(0) type of event.

I tried implementing this by using an if statement like:

int result = JOptionPane.showOptionDialog(null, getPanel(),"Return Builder", JOptionPane.OK_CANCEL_OPTION, 
                JOptionPane.PLAIN_MESSAGE, null, options, "default");

        if(result==JOptionPane.CLOSED_OPTION){
            System.exit(0);
        }

but I find that the entire program exits regardless of which button the user clicks. Even if they click one of my other JButtons that has an action listener the program still quits. I'll post the more complete picture of this particular GUI:

public static void displayGUI(){//Method to display the GUI. 

        final JButton buttonCreate = new JButton("Create Return");
        final JButton buttonConfirm = new JButton("Confirm");

        buttonConfirm.addActionListener(new ActionListener(){

            public void actionPerformed(final ActionEvent ae){


                if(output.getSize()>0){
                    JOptionPane.getRootFrame().dispose();   
                }else if(verifyBatch==true){
                    JOptionPane.getRootFrame().dispose();
                }else if(verifyBatch==false && output.getSize()==0){
                    JOptionPane.showMessageDialog(null, "You haven't added any transactions to confirm" +
                            " and you have no previously completed batches!");
                }
            }           
        });

        buttonCreate.addActionListener(new ActionListener(){

            public void actionPerformed(final ActionEvent ae){

                if(verifyBatch==true){

                    initialScreenDecisions="NONE";//The user did not choose to add any entry details to the output list.
                    MainWriter.finishedCounter=true;//The boolean counter to trigger that the return is finished goes to true.
                    while(MainWriter.entryDetails.size()>0){//Removes all entry details from the input list.
                        MainWriter.entryDetails.remove(0);
                    }

                    while(output.size()>0){//Removes all entry details from the output list..
                        output.remove(0);
                    }

                    JOptionPane.getRootFrame().dispose();

                }else{

                    JOptionPane.showMessageDialog(null, "There are no completed batches!");     

                }
            }
        });

        //Creates a JOptionPane for the first GUI featuring 7 buttons and 2 lists..

        final Object[] options = new Object[] {buttonConfirm,buttonCreate};

        int result = JOptionPane.showOptionDialog(null, getPanel(),"Return Builder", JOptionPane.OK_CANCEL_OPTION, 
                JOptionPane.PLAIN_MESSAGE, null, options, "default");

        if(result==JOptionPane.CLOSED_OPTION){
            System.exit(0);
        }
    }

So from this example, how can I make the "X" and only the "X" exit the entire program? I have many other JOptionsPanes that I need to implement similar measures.


Solution

  • with "X" I want to cause a System.exit(0) type of event.

    First of all, the JVM will automatically exit after your last window (JOptionPane) closes. This is probably the reason for

    Even if they click one of my other JButtons that has an action listener the program still quits.

    Secondly, users usually do not expect that closing a dialog will exist the program, think about it from a GUI design point of view. You usually have a parent JFrame and the dialogs supplement it, they are not the "driving force" of the GUI.

    If I create a frame just to stop the auto-closing of the JVM, you will see that the JOptionPane does give different return values for pressing the "X" and for other options:

    public static void main(String[] args) {
    
        JFrame frame = new JFrame();
        frame.setVisible(true);
        int result = JOptionPane.showConfirmDialog(null, "AA");
        System.out.println(result);
        if (result == JOptionPane.CLOSED_OPTION)
            System.exit(0);
    }