I want to open a new window, from a JMenu "File Open" command.

Posted on 2005-04-12
Last Modified: 2008-01-09
I am trying open a new window, with dates in it from a JMenu "file open" .
I am attaching my menu class code.

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class MyMenu implements ActionListener{
     public JMenuBar menuBar = new JMenuBar();
     private JMenuItem mi[],mnuDates,mnuParts,mnumfgOps,mnuExit,mnuContracts,mnuQuotations;

     public MyMenu(){
                      JMenu fileMenu = new JMenu("File");

           JMenu mnuReports = new JMenu("Reports");

           JMenu mnuQuoteStats = new JMenu("Help");
           mnuDates = new JMenuItem("Open Date File");

           mnuParts = new JMenuItem("Print");
           mnumfgOps = new JMenuItem("Save Plan Data");
           mnuQuotations = new JMenuItem("Open Quotations");
           mnuContracts = new JMenuItem("Contracts Received");

           mnuExit = new JMenuItem("Exit");


     public JMenuBar getMenuBar(){
          return menuBar;
     public void actionPerformed(ActionEvent e){

Question by:DanFlyBoy
    LVL 1

    Expert Comment

    You're gonna want to add MenuListeners to each of the menus above. Once you do, find out which of the JMenuItems is selected (getSelectedObjects)
    LVL 1

    Accepted Solution


    I dunno if all my parentheses match or not, but I believe this will work:
        fileMenu.addMenuListener (
          new MenuListener () {
            public void menuSelected (MenuEvent e) {
              if (((JMenuItem)((JMenu)e.getSource ()).getSelectedObjects ()) == mnuDates) {
                /// do stuff here
            // other MenuListener functions here

    Author Comment

    When i click the open button on the menu bar, I want a new window to apper, I am still unable to do this even though I added the MenuListerner.
    LVL 1

    Expert Comment

    Inside the MenuListener, you need to create a JFrame and show it.

    Author Comment

    I still have not gotten the menulisterner to work.  Any ideas?
    LVL 5

    Expert Comment

    class myFrame extends Frame implements ActionListener

         public myFrame()
                  MenuItem newWindowItem;
                  MenuBar myMenuBar = new MenuBar();    // create the menu bar
                  Menu windowMenu = new Menu("window"); // create the menu
                  newWindowItem = new MenuItem("add new window");   // create the menu item
                  newWindowItem.addActionListener(this);  // add the listener for new window
                   // add it to the Menu
         public void ActionPerformed(ActionEvent e)
                if(e.getSource() == newWindowItem)
                {       // if new window menu item is selected
                        Frame f = new myFrame();
                        // create the new window

    Author Comment

    I tried the class Frame code, I could not get it to run, also, how would I implement this in my code that I published?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Live - One-on-One Java Help from Top Experts

    Solve your toughest problems, fast.
    Java experts are online now and ready to help you.

    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    The viewer will learn how to implement Singleton Design Pattern in Java.

    794 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

    16 Experts available now in Live!

    Get 1:1 Help Now