JMenu and invoking close the window

I created JFrame in the applet, but JMenu is on applet.(It supposed to be on JFrame.)
The following is my code segment.
------------------------------------------
public class LibraryApplet extends JApplet implements ActionListener{
      …
      …
  public void init() {
  myFrame = new JFrame();
  contentPane = myFrame.getContentPane();
  layout = new GridBagLayout();
  contentPane.setLayout(layout);
  constraints = new GridBagConstraints();
       …
       …
   JMenu fileMenu = new JMenu("File");
   JMenuItem exitItem = new JMenuItem("Exit");
   fileMenu.add(exitItem);
    JMenuBar bar = new JMenuBar();
    setJMenuBar(bar);
    bar.add(fileMenu);

    myFrame.pack() ;
  }
--------------------------------------
Another question is that top-level window(JFrame) has a File menu containing a menu item "Exit". When "Exit" is invoked the application will close the window (the user sees an empty applet page with just the title and header)

 How do I implement the above "Exit" behavior?

Thank you for help
dkim18Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

yongsingCommented:
Change

setJMenuBar(bar);

to

myFrame.setJMenuBar(bar);
yongsingCommented:
>> How do I implement the above "Exit" behavior?

How do you want the "Exit" behaviour to be like?
dkim18Author Commented:
When "Exit" is invoked the application will close the window (the user sees an empty applet page with just the title and header)
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Mick BarryJava DeveloperCommented:
myFrame.hide();
yongsingCommented:
Add this to your code and see if it helps:

myFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
yongsingCommented:
Ignore my last comment. objects is right. You need to call JFrame.hide(). You also need to add action listener to your exit menu item:

exitItem.addActionListener(
  new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      ((JFrame)e.getSource()).hide();
    }
  }
);
Mick BarryJava DeveloperCommented:
The window shouldn't be disposed cause the next time the applet is loaded the same window is used.
Mick BarryJava DeveloperCommented:
shouldn't that be:

exitItem.addActionListener(
  new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      myFrame.hide();
    }
  }
);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
yongsingCommented:
Oh ya, that was a mistake. :-)
dkim18Author Commented:
objects
superb!
Mick BarryJava DeveloperCommented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.