-1

I'm trying to exit my application by choosing the exit-option in the menu bar, but I need to get a prompt before the program close.

I have tried to find the answer here by reading similar questions, but many solutions just ends with "System.exti(0)" in the actionListener of the menuItem. I need to get a message dialog before the application is closed down.

Here I have declared the menu bar and you can see the exit-option:

JMenuBar menuBar = new JMenuBar();
    JMenu archiveMenu = new JMenu("Archive");
    menuBar.add(archiveMenu);
    newMap = archiveMenu.add("New Map");
    newMap.addActionListener(new ArchiveListener());
    loadPlaces = archiveMenu.add("Load Places");
    loadPlaces.addActionListener(new ArchiveListener());
    save = archiveMenu.add("Save");
    save.addActionListener(new ArchiveListener());
    exit = archiveMenu.add("Exit");
    exit.addActionListener(new ArchiveListener());

And here is my code from the ArchiveListener. You can see that I've tried to get the exit-option to work in the code at very the bottom, but this solution doesn't work:

 class ArchiveListener implements ActionListener {

    public void actionPerformed(ActionEvent ave) {
        if (ave.getSource() == newMap) {
            int result = fc.showOpenDialog(null);
            if (result != JFileChooser.APPROVE_OPTION) {
                return;
            } else {
                File file = fc.getSelectedFile();
                String filePath = file.getAbsolutePath();
                display.setImage(filePath);
                validate();
                repaint();
            }

        } else if (ave.getSource() == loadPlaces) {

            int result = fc.showOpenDialog(null);
            if (result != JFileChooser.APPROVE_OPTION) {
                return;
            } else {
                loadPlaces();
            }
        }
        if (ave.getSource() == save) { 
            if (fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
                // File file = fc.getSelectedFile();
            }
            try {
                FileWriter outfile = new FileWriter("jarvafaltet.places.txt");
                PrintWriter out = new PrintWriter(outfile);
                for (Place p : positionList.values()) {
                    System.out.println(p);
                    out.println(p);
                    out.close();
                }

                outfile.close();

            } catch (FileNotFoundException e) {
                JOptionPane.showMessageDialog(null, "Filen kan ej hittas");

            } catch (IOException e) {
                JOptionPane.showMessageDialog(null, "Fel" + e.getMessage());

            }

        }

        if ("exit".equals(ave.getActionCommand())) {

            int dialogButton = JOptionPane.YES_NO_OPTION;
            JOptionPane.showConfirmDialog(null, "Would You Like to Save your Previous Note First?", "Warning", dialogButton);

            if (dialogButton == JOptionPane.YES_OPTION) {
                System.exit(NORMAL);
            }

        }

    }
Vicky123
  • 1
  • 3

1 Answers1

1

Your code needs to read the value returned by showConfirmDialog:

int dialogButton = JOptionPane.showConfirmDialog(null, "Would You Like to Save your Previous Note First?", "Warning", JOptionPane.YES_NO_OPTION);
if (dialogButton == JOptionPane.YES_OPTION) {
    System.exit(NORMAL);
}

and you need to read the javadoc :o)

Hugues M.
  • 19,846
  • 6
  • 37
  • 65