?
Solved

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

Posted on 2005-04-12
7
Medium Priority
?
747 Views
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");
           fileMenu.setMnemonic('F');
           fileMenu.setBackground(java.awt.Color.LIGHT_GRAY);

           JMenu mnuReports = new JMenu("Reports");
           mnuReports.setMnemonic('R');
           mnuReports.setBackground(java.awt.Color.LIGHT_GRAY);

           JMenu mnuQuoteStats = new JMenu("Help");
           mnuQuoteStats.setMnemonic('S');
           mnuQuoteStats.setBackground(java.awt.Color.LIGHT_GRAY);
           
           mnuDates = new JMenuItem("Open Date File");
           mnuDates.setMnemonic('Q');
           mnuDates.setBackground(java.awt.Color.LIGHT_GRAY);
           
           fileMenu.add(mnuDates);

           mnuParts = new JMenuItem("Print");
           mnuParts.setMnemonic('P');
           mnuParts.setBackground(java.awt.Color.LIGHT_GRAY);
           mnuParts.addActionListener(this);
           fileMenu.add(mnuParts);
           mnumfgOps = new JMenuItem("Save Plan Data");
           mnumfgOps.setMnemonic('M');
           mnumfgOps.setBackground(java.awt.Color.LIGHT_GRAY);
           mnumfgOps.addActionListener(this);
           fileMenu.add(mnumfgOps);
           fileMenu.add(mnuReports);
           fileMenu.setBorderPainted(false);
           mnuQuotations = new JMenuItem("Open Quotations");
           mnuQuotations.setMnemonic('U');
           mnuQuotations.setBackground(java.awt.Color.LIGHT_GRAY);
           mnuQuotations.addActionListener(this);
           mnuQuoteStats.add(mnuQuotations);
           mnuContracts = new JMenuItem("Contracts Received");
           mnuContracts.setMnemonic('C');
           mnuContracts.setBackground(java.awt.Color.LIGHT_GRAY);
           mnuQuoteStats.add(mnuContracts);

           mnuExit = new JMenuItem("Exit");
           mnuExit.setMnemonic('X');
           mnuExit.setBackground(java.awt.Color.LIGHT_GRAY);
           mnuExit.addActionListener(this);
           fileMenu.add(mnuExit);

           menuBar.setBackground(java.awt.Color.LIGHT_GRAY);
           menuBar.add(fileMenu);
           menuBar.add(mnuReports);
           menuBar.add(mnuQuoteStats);
         
     }

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

0
Comment
Question by:DanFlyBoy
  • 3
  • 3
7 Comments
 
LVL 1

Expert Comment

by:VolatileVoid
ID: 13762574
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)
0
 
LVL 1

Accepted Solution

by:
VolatileVoid earned 225 total points
ID: 13763116
Example--

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
      }
    );
0
 

Author Comment

by:DanFlyBoy
ID: 13763585
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:VolatileVoid
ID: 13764663
Inside the MenuListener, you need to create a JFrame and show it.
0
 

Author Comment

by:DanFlyBoy
ID: 13765013
I still have not gotten the menulisterner to work.  Any ideas?
0
 
LVL 5

Expert Comment

by:ashok3sep
ID: 13765189
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
              windowMenu.add(newWindowItem);
              myMenuBar.add(windowMenu);
              setMenuBar(myMenuBar);
     }
     public void ActionPerformed(ActionEvent e)
     {
            if(e.getSource() == newWindowItem)
            {       // if new window menu item is selected
                    Frame f = new myFrame();
                    f.setSize(100,100);
                    f.setVisible(true);                  
                    // create the new window
            }
      }
}
0
 

Author Comment

by:DanFlyBoy
ID: 13767337
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?
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

749 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