Solved

JMenu and invoking close the window

Posted on 2003-11-26
11
605 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:dkim18
  • 5
  • 4
  • 2
11 Comments
 
LVL 9

Assisted Solution

by:yongsing
yongsing earned 50 total points
ID: 9830114
Change

setJMenuBar(bar);

to

myFrame.setJMenuBar(bar);
0
 
LVL 9

Expert Comment

by:yongsing
ID: 9830116
>> How do I implement the above "Exit" behavior?

How do you want the "Exit" behaviour to be like?
0
 

Author Comment

by:dkim18
ID: 9830138
When "Exit" is invoked the application will close the window (the user sees an empty applet page with just the title and header)
0
 
LVL 92

Expert Comment

by:objects
ID: 9830154
myFrame.hide();
0
 
LVL 9

Expert Comment

by:yongsing
ID: 9830163
Add this to your code and see if it helps:

myFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Expert Comment

by:yongsing
ID: 9830170
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();
    }
  }
);
0
 
LVL 92

Expert Comment

by:objects
ID: 9830171
The window shouldn't be disposed cause the next time the applet is loaded the same window is used.
0
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 9830176
shouldn't that be:

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

Expert Comment

by:yongsing
ID: 9830198
Oh ya, that was a mistake. :-)
0
 

Author Comment

by:dkim18
ID: 9832697
objects
superb!
0
 
LVL 92

Expert Comment

by:objects
ID: 9833720
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB Script to add site to Java Exception List 4 53
how to add new optional parameter to JSP 1 29
difference of if loops 23 43
Way to decrease size of apk file 9 46
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now