Solved

JMenu and invoking close the window

Posted on 2003-11-26
11
604 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
Comment Utility
Change

setJMenuBar(bar);

to

myFrame.setJMenuBar(bar);
0
 
LVL 9

Expert Comment

by:yongsing
Comment Utility
>> How do I implement the above "Exit" behavior?

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

Author Comment

by:dkim18
Comment Utility
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
Comment Utility
myFrame.hide();
0
 
LVL 9

Expert Comment

by:yongsing
Comment Utility
Add this to your code and see if it helps:

myFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 9

Expert Comment

by:yongsing
Comment Utility
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
Comment Utility
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
Comment Utility
shouldn't that be:

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

Expert Comment

by:yongsing
Comment Utility
Oh ya, that was a mistake. :-)
0
 

Author Comment

by:dkim18
Comment Utility
objects
superb!
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

744 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

15 Experts available now in Live!

Get 1:1 Help Now