Link to home
Start Free TrialLog in
Avatar of dkim18
dkim18

asked on

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
SOLUTION
Avatar of yongsing
yongsing

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yongsing
yongsing

>> How do I implement the above "Exit" behavior?

How do you want the "Exit" behaviour to be like?
Avatar of dkim18

ASKER

When "Exit" is invoked the application will close the window (the user sees an empty applet page with just the title and header)
Avatar of Mick Barry
myFrame.hide();
Add this to your code and see if it helps:

myFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
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();
    }
  }
);
The window shouldn't be disposed cause the next time the applet is loaded the same window is used.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh ya, that was a mistake. :-)
Avatar of dkim18

ASKER

objects
superb!