3
public class Manubar extends JFrame {

    JMenuBar jmb;
    JMenu jm;
    JMenu jm2;
    JMenuItem jmt;
    JMenuItem jmt2;

    public Manubar() {
        setSize(500, 500);

        jmb = new JMenuBar();
        jm = new JMenu("file");
        jm2 = new JMenu("edit");
        jmt = new JMenuItem("copy");
        jmt2 = new JMenuItem("exit");
        jmb.add(jm);
        jmb.add(jm2);
        jm.add(jmt);
        jm.add(jmt2);
        add(jmb, BorderLayout.NORTH);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new Manubar();
    }
}

Here I want to close the window when I click on exit menu item, also before closing, it should display a popup to ask whether to close if user clicks OK then it should close.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
chris
  • 141
  • 2
  • 8
  • 2
    What you have tried to do so?? – Sindhoo Oad Oct 08 '15 at 13:33
  • i am new to swings was using actionListener on exit menuitem but i am confused how to use JOptionPane to popup and when i click ok the frame should close – chris Oct 08 '15 at 13:35
  • 2
    Add an actionlistner and on onclick show a joptionpane and on bases of its value exit from the frame. Try it then what problem you are facing share it. Then we will help you. – Sindhoo Oad Oct 08 '15 at 13:38
  • 1
    1) It is Swing, not Swings. 2) Go through [How to Make Dialogs](https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html). 3) Please be more specific in future about what you've tried, and where you are stuck. SO is a Q&A site, not a help desk & not a place to find a tutor.. – Andrew Thompson Oct 08 '15 at 13:38

2 Answers2

4

Here I want to close the window when I click on exit menu item, also before closing, it should display a popup to ask whether to close if user clicks OK then it should close.

Check out Closing an Application. It shows you how to display a JOptionPane to confirm closing of the application first.

It shows:

  1. the basic approach of using a WindowListener
  2. a simplified approach by using the included custom classes
camickr
  • 321,443
  • 19
  • 166
  • 288
3

Here is your complete solution,

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Manubar extends JFrame implements ActionListener {

    JMenuBar jmb;
    JMenu jm;
    JMenu jm2;
    JMenuItem jmt;
    JMenuItem jmt2;

    public Manubar() {
        setSize(500, 500);

        jmb = new JMenuBar();
        jm = new JMenu("file");
        jm2 = new JMenu("edit");
        jmt = new JMenuItem("copy");
        jmt2 = new JMenuItem("exit");
        jmb.add(jm);
        jmb.add(jm2);
        jm.add(jmt);
        jm.add(jmt2);
        add(jmb, BorderLayout.NORTH);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jmt2.addActionListener(this);
    }

    public static void main(String[] args) {
        new Manubar();
    }

    @Override
    public void actionPerformed(ActionEvent e) {

       if("exit".equals(e.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);
         }

    }

    }

}
Vishal Gajera
  • 4,137
  • 5
  • 28
  • 55