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(layo ut);
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
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(layo
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
When "Exit" is invoked the application will close the window (the user sees an empty applet page with just the title and header)
myFrame.hide();
Add this to your code and see if it helps:
myFrame.setDefaultCloseOpe ration(Win dowConstan ts.DISPOSE _ON_CLOSE) ;
myFrame.setDefaultCloseOpe
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(ActionEven t e) {
((JFrame)e.getSource()).hi de();
}
}
);
exitItem.addActionListener
new ActionListener() {
public void actionPerformed(ActionEven
((JFrame)e.getSource()).hi
}
}
);
The window shouldn't be disposed cause the next time the applet is loaded the same window is used.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Oh ya, that was a mistake. :-)
ASKER
objects
superb!
superb!
How do you want the "Exit" behaviour to be like?