?
Solved

JMenu and invoking close the window

Posted on 2003-11-26
11
Medium Priority
?
613 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 9

Assisted Solution

by:yongsing
yongsing earned 200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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 800 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

801 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