JMenubar and JMenuIteam

Hi frds,
I add the JMenu after that i add JMenuIteam.
In add Three menuiteam..
so which event i will for that..?
if i will click on one menuiteam then open one iteam like that so which event i wil add for that.?
so please give me brief information.
Thank you,

Hi frds,
I add the JMenu after that i add JMenuIteam.
In add Three menuiteam..
so which event i will for that..?
if i will click on one menuiteam then open one iteam
like that so which event i wil add for that.?
so please give me brief information.
Thank you,(only 50% sure what your talking about)
try implementing ActionListener and register your menu items using addActionListener

Similar Messages

  • JMenuBar and JMenuItem

    I need to have a JMenuItem in a JMenuBar but I'm having difficulties. If I have a JMenu with no children JMenuItems below it and I add that JMenu to the JMenuBar, I get a small square below the JMenu when I click it. I tried to add a JMenuItem to the JMenuBar in place of the childless JMenu but when I do that, the JMenuItem spans the width of the JMenuBar and that isn't good. Is there a way to have a JMenuItem as a part of a JMenuBar without the adverse effects described just now? Thanks, Jeremy

    I used gridBagLayout to do it as follows
    ivjTestFrameJMenuBar = new javax.swing.JMenuBar();
                   ivjTestFrameJMenuBar.setLayout(new GridBagLayout());
                   ivjTestFrameJMenuBar.setName("TestFrameJMenuBar");
                   java.awt.GridBagConstraints constraintsJMenu1 = new java.awt.GridBagConstraints();
                   constraintsJMenu1.gridx = 0; constraintsJMenu1.gridy = 0;
                   constraintsJMenu1.insets = new java.awt.Insets(4, 4, 4, 4);
                   ivjTestFrameJMenuBar.add(getJMenu1(), constraintsJMenu1);
                   java.awt.GridBagConstraints constraintsJMenuItem = new java.awt.GridBagConstraints();
                   constraintsJMenuItem.gridx = 1; constraintsJMenuItem.gridy = 0;
                   constraintsJMenuItem.insets = new java.awt.Insets(4, 4, 4, 4);
                   ivjTestFrameJMenuBar.add(new javax.swing.JMenuItem("Item 1"), constraintsJMenuItem);
                   java.awt.GridBagConstraints constraintsJMenu2 = new java.awt.GridBagConstraints();
                   constraintsJMenu2.gridx = 2; constraintsJMenu2.gridy = 0;
                   constraintsJMenu2.anchor = java.awt.GridBagConstraints.WEST;
                   constraintsJMenu2.weightx = 1.0;
                   constraintsJMenu2.insets = new java.awt.Insets(4, 4, 4, 4);
                   ivjTestFrameJMenuBar.add(getJMenu2(), constraintsJMenu2);see how it works for you, it seemed to work ok for me
    Sok

  • How to add a JMenubar and a JTable in a JFrame in a single application

    Hi all,
    I require an urgent help from you.I am a beginer in programming Swing.I want to add a menu,combobox,and a table in a single application.I did coding as below:
    package com.BSS;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.*;
    import javax.swing.border.*;
    import javax.swing.event.*;
    public class newssa extends JFrame
         public JMenuBar menuBar;
         public JToolBar toolBar;
         public JFrame frame;
         private JLabel jLabel1;
         private JLabel jLabel2;
         private JLabel jLabel3;
         private JLabel jLabel4;
         private JLabel jLabel5;
         private JLabel jLabel6;
         private JComboBox jComboBox1;
         private JComboBox jComboBox2;
         private JComboBox jComboBox3;
         private JComboBox jComboBox4;
         private JComboBox jComboBox5;
         private JComboBox jComboBox6;
         private JTable jTable1;
         private JScrollPane jScrollPane1;
         private JPanel contentPane;
         public newssa()
              super();
              initializeComponent();
              this.setVisible(true);
         private void initializeComponent()
              jLabel1 = new JLabel();
              jLabel2 = new JLabel();
              jLabel3 = new JLabel();
              jLabel4 = new JLabel();
              jLabel5 = new JLabel();
              jLabel6 = new JLabel();
              jComboBox1 = new JComboBox();
              jComboBox2 = new JComboBox();
              jComboBox3 = new JComboBox();
              jComboBox4 = new JComboBox();
              jComboBox5 = new JComboBox();
              jComboBox6 = new JComboBox();
              frame=new JFrame();
              //Included here
              JMenuBar menuBar = new JMenuBar();
              JMenu general = new JMenu("General");
         menuBar.add(general);
         JMenu actions =new JMenu("Actions");
         menuBar.add(actions);
         JMenu view=new JMenu("View");
         menuBar.add(view);
         JMenu Timescale=new JMenu("TimeScale");
         menuBar.add(Timescale);
         Timescale.add("Today CTRL+D");
         Timescale.add("Current Week CTRL+W");
         Timescale.add("Current Month CTRL+M");
         Timescale.add("Current Quarter CTRL+Q");
         Timescale.add("Current Year CTRL+Y");
         Timescale.add("Custom TimeScale CTRL+U");
         JMenu start=new JMenu("Start");
         menuBar.add(start);
         JMenu options=new JMenu("Options");
         menuBar.add(options);
         JMenu help=new JMenu("Help");
         menuBar.add(help);
         JFrame.setDefaultLookAndFeelDecorated(true);
         frame.setJMenuBar(menuBar);
         frame.pack();
         frame.setVisible(true);
         toolBar = new JToolBar("Formatting");
         toolBar.addSeparator();
              //Before this included new
              String columnNames[] = { "ColorStatus", "Flash", "Service Order","Configuration","Configuration Description"};
              // Create some data
              String dataValues[][] =
                   { "blue", "flash", "ORT001" },
                   { "AVCONF", "av configuration with warrenty"}
              // Create a new table instance
              //jTable1 = new JTable( dataValues, columnNames );
              jTable1 = new JTable(dataValues,columnNames);
              jScrollPane1 = new JScrollPane(jTable1);
              contentPane = (JPanel)this.getContentPane();
              //scrollPane = new JScrollPane( table );
              //topPanel.add( scrollPane, BorderLayout.CENTER );
              // jLabel1
              jLabel1.setText("Service Centers");
              // jLabel2
              jLabel2.setText("Service Areas");
              // jLabel4
              jLabel3.setText("Skills");
              jLabel4.setText("Availablity Types");
              // jLabel5
              jLabel5.setText("From Date");
              // jLabel6
              jLabel6.setText("To");
              // jComboBox1
              jComboBox1.addItem("Coimbatore");
              jComboBox1.addItem("Chennai");
              jComboBox1.addItem("Mumbai");
              jComboBox1.addItem("New Delhi");
              jComboBox1.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e)
                        jComboBox1_actionPerformed(e);
              // jComboBox2
              jComboBox2.addItem("North Zone");
              jComboBox2.addItem("South Zone");
              jComboBox2.addItem("Central Zone");
              jComboBox2.addItem("Eastern Zone");
              jComboBox2.addItem("Western Zone");
              jComboBox2.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e)
                        jComboBox2_actionPerformed(e);
              // jComboBox3
              jComboBox3.addItem("Microsoft Components");
              jComboBox3.addItem("Java Technologies");
              jComboBox3.addItem("ERP");
              jComboBox3.addItem("Others");
              jComboBox3.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e)
                        jComboBox3_actionPerformed(e);
              // jComboBox4
              jComboBox4.addItem("One");
              jComboBox4.addItem("Two");
              jComboBox4.addItem("Three");
              jComboBox4.addItem("Four");
              jComboBox4.addItem("Five");
              jComboBox4.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e)
                        jComboBox4_actionPerformed(e);
              // jComboBox5
              jComboBox5.addItem("12/12/2004");
              jComboBox5.addItem("13/12/2004");
              jComboBox5.addItem("14/12/2004");
              jComboBox5.setEditable(true);
              jComboBox5.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e)
                        jComboBox5_actionPerformed(e);
              // jComboBox6
              jComboBox6.addItem("12/11/2004");
              jComboBox6.addItem("13/11/2004");
              jComboBox6.addItem("14/11/2004");
              jComboBox6.setEditable(true);
              jComboBox6.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e)
                        jComboBox6_actionPerformed(e);
              // jTable1
              jTable1.setModel(new DefaultTableModel(4, 4));
              // jScrollPane1
              jScrollPane1.setViewportView(jTable1);
              // contentPane
              contentPane.setLayout(null);
              addComponent(contentPane, jLabel1, 2,29,84,18);
              addComponent(contentPane, jLabel2, 201,33,76,18);
              addComponent(contentPane, jLabel3, 384,32,59,18);
              addComponent(contentPane, jLabel4, 2,77,85,18);
              addComponent(contentPane, jLabel5, 197,79,84,18);
              addComponent(contentPane, jLabel6, 384,80,60,18);
              addComponent(contentPane, jComboBox1, 85,32,100,22);
              addComponent(contentPane, jComboBox2, 276,32,100,22);
              addComponent(contentPane, jComboBox3, 419,30,100,22);
              addComponent(contentPane, jComboBox4, 88,76,100,22);
              addComponent(contentPane, jComboBox5, 276,79,100,22);
              addComponent(contentPane, jComboBox6, 421,78,100,22);
              addComponent(contentPane, jScrollPane1, 33,158,504,170);
              // newssa
              this.setTitle("SSA Service Scheduler");
              this.setLocation(new Point(0, 0));
              this.setSize(new Dimension(560, 485));
         /** Add Component Without a Layout Manager (Absolute Positioning) */
         private void addComponent(Container container,Component c,int x,int y,int width,int height)
              c.setBounds(x,y,width,height);
              container.add(c);
         // TODO: Add any appropriate code in the following Event Handling Methods
         private void jComboBox1_actionPerformed(ActionEvent e)
              int index = jComboBox1.getSelectedIndex();
              switch(index)
                   case 0: System.out.println("Area Coimbatore Selected "); break;
                   case 1: System.out.println("Area Chennai selected"); break;
                   case 2: System.out.println("Mumbai being selected"); break;
                   case 3: System.out.println("New Delhi being selected"); break;
         private void jComboBox2_actionPerformed(ActionEvent e)
              int index = jComboBox2.getSelectedIndex();
              switch(index)
                   case 0: System.out.println("North Zone Selcted "); break;
                   case 1: System.out.println("South Zone being selected"); break;
                   case 2: System.out.println("Central Zone being selected"); break;
                   case 3: System.out.println("Eastern Zone being selected"); break;
                   case 4: System.out.println("Western Zone being selected"); break;
         private void jComboBox3_actionPerformed(ActionEvent e)
              int index = jComboBox3.getSelectedIndex();
              switch(index)
                   case 0: System.out.println("Microsoft Components being selected"); break;
                   case 1: System.out.println("Java Technologies being selected"); break;
                   case 2: System.out.println("ERP Tehnologies being selected"); break;
                   case 3: System.out.println("Other's selected"); break;
         private void jComboBox4_actionPerformed(ActionEvent e)
              int index = jComboBox4.getSelectedIndex();
              switch(index)
                   case 0: System.out.println("One selected"); break;
                   case 1: System.out.println("Two selected"); break;
                   case 2: System.out.println("Three selected"); break;
                   case 3: System.out.println("Four selected"); break;
                   case 4: System.out.println("Five selected"); break;
         private void jComboBox5_actionPerformed(ActionEvent e)
              int index = jComboBox5.getSelectedIndex();
              switch(index)
                   case 0: System.out.println("12/12/2004 being selected"); break;
                   case 1: System.out.println("13/12/2004 being selected"); break;
                   case 2: System.out.println("14/12/2004 being selected"); break;
         private void jComboBox6_actionPerformed(ActionEvent e)
              int index = jComboBox6.getSelectedIndex();
              switch(index)
                   case 0: System.out.println("12/11/2004 being selected"); break;
                   case 1: System.out.println("13/11/2004 being selected"); break;
                   case 2: System.out.println("14/11/2004 being selected"); break;
         public static void main(String[] args)
              newssa ssa=new newssa();
              //JFrame.setDefaultLookAndFeelDecorated(true);
              //JDialog.setDefaultLookAndFeelDecorated(true);
              //JFrame frame = new JFrame("SSA Service Scheduler");
    //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //frame.setJMenuBar(ssa.menuBar);
    //frame.getContentPane( ).add(ssa.toolBar, BorderLayout.NORTH);
    //frame.getContentPane( ).add(ssa.pane, BorderLayout.CENTER);
    //frame.pack( );
    //frame.setVisible(true);
              try
                   //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                   UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
              catch (Exception ex)
                   System.out.println("Failed loading L&F: ");
                   System.out.println(ex);
    But as a O/P ,I am getting menu in a seperate windos and the rest of the combobox and jtable in a seperate window.Kindly help me to solve the application.
    VERY URGENT PLEASE..
    Thanks in advance
    with kind regds
    Satheesh.K

    But did u mean this as the next problem,Which I will come across..Yes, the second setVisible(true) seemed to be producing a smaller frame behind the main frame.
    And your JMenuBar is declared twice, but not sure if this will affect the code - haven't read it all.

  • JMenuBar and popup message

    hello, I am using the JMenuBar for my simple program, I got two menus to the left, Figures and colors, and to the right I have a About box, this about box is supposed to show me Info on top of the drawingboard,�I want to be able to hardtype some text into to messagebox/popupbox but I cant find the appropiate method...anyone got an idea?
    sincerarly,

    Use a JOptionPane. Look at the JOptionPane section here:
    http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/
    colr__

  • JMenuBar and JButton problem

    The problem is a simple one.
    Simply I don't know how to have the two together, every time I try to create a contentPane with the buttons in it I always get errors I would love it if some one could show me how to do this.
    Thanx

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    public class Test extends JFrame {
      public Test() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container content = getContentPane();
        content.setLayout(new FlowLayout());
        JMenuBar jmb = new JMenuBar();
        setJMenuBar(jmb);
        JMenu jm = new JMenu("Menu");
        jmb.add(jm);
        for (int i=0; i<5; i++) {
          JMenuItem jmi = new JMenuItem("MenuItem-"+i);
          jm.add(jmi);
          jmi.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
              System.out.println(((JMenuItem)ae.getSource()).getText());
          JButton jb = new JButton("Button-"+i);
          content.add(jb);
          jb.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
              System.out.println(((JButton)ae.getSource()).getText());
        setSize(200,400);
        show();
      public static void main(String[] args) { new Test(); }
    }

  • Java.awt.Image getWidth() and getHeight() not working!

    Right- this is want I want to do.
    I have a background image which is added as a background image to a third party API class which extends Java.awt.Container. Such container is then added to the Content Pane of MyClass (which extends JFrame)
    I want to read the width and height of the image and resize MyClass accordingly.
    Problem is calling theImage.getWidth(MyClass.this) or theImage.getWidth(theThirdPartyAPIContainer) always return -1 and -1 denoting that no information is available.
    What do I need to do to obtain the height and width?

    Post here, cos don't know your email.
    Anyway
    - Can't use setPreferredSize because the Conatiner which is added to the ContentPane is a Awt Component not a Swing JComponent!
    - Also, the background is added AFTER the this.pack()and this.show().
    -I figured out that I could access Insets.top, Insets.left, Insets.right, Insets.bottom.
    I have basically the following
    |JMenuBar |
    L____________________________________________|
    | |
    | |
    | |
    | Third Party Class extending java.awt.Container |
    | containing an image of known size |
    | |
    L____________________________________________|
    I am fine for the width I do
    Insets frameInsets = this.getInsets();
    width = image.getWidth() + frameInsets.left + frameInsets.right;
    Height is the problem
    After some Trial an error it LOOKS on Solaris as if the following is right-
    height = image.getHeight()+frameInsets.top+ myJMenuBar.getSize()+getHeight+ myJMenuBar.getInsets().top + myJMenuBar.getInsets().bottom
    I cannot comprehend whether this is just a coincidence or not as I don't know why frameInsets.bottom should in any case be excluded.
    Here are the dimensions-
    frameInsets.left = 6
    frameInsets.right= 6
    frameInsets.top =28
    frameInsets.bottom = 6
    JMenuBar height = 23
    JMenuBar's Insets.top = 1
    JMenuBar's Insets.bottom = 1
    What should be the 'correct' calculations and does the Insets for the JMenuBar and the JFrame overlaps?

  • How can I distinguish a JMenuItem , JPopupMenu and TopLevelMenu?

    I used the following methods to get the component who (a menu)gains the focus:
    JComponent com = JFrmame.getFocusOwner();
    or
    MenuSelectionManager menuMgr = MenuSelectionManager.defaultManager();
    MenuElement[] menuPath = menuMgr.getSelectedPath();
    if(menuPath != 0) {
    JComponent comp = Array.get(menuPath, menuPath.length-1); // this is the item with the focus
    then I need to use getAction() to get the menuItem's action. But how can I filter JMenuItem and JPopUpMenu from TopLevelMenu (JMenuBar item) ?
    Thanks!

    The instanceof may provide the simplest solution, since JPopupMenu, JMenuBar and JMenuItem don't have any kind of ancestor relationship with each other.if (comp instanceof JPopupMenu) {
        // Popup
    } else if (comp instanceof JMenuItem) {
        // Item
    } else if (comp instanceof JMenuBar) {
        // Bar
    } else ...?Hope this helps,
    -Troy

  • Menus, Buttons and AbstractAction

    I am using the code below to create both a JMenuBar and JToolBar.
    When I clcik on the Menu item "Cut", the println shows cmd: Cut, which is the value from NAME.
    When I click on the ToolBar button, the println shows cmd: null. WHY?
    Have I failed to do something to enable the event to show Cut?
    Could there be a problem in the manner in which I create the JToolBar that initializes the NAME varibale to null?
    The SHORT_DESCRIPTION works and the SMALL_ICON is displayed properly for the button.
    private Action m_cut = new AbstractAction() {
    {     putValue(NAME, "Cut");
         Icon icon = new ImageIcon(getClass().getResource("pics/Cut24.gif"));
         putValue(SMALL_ICON, icon);
         putValue(SHORT_DESCRIPTION, "Cut selected object");
         putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_U));
         putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(
         KeyEvent.VK_X,InputEvent.CTRL_DOWN_MASK));
         public void actionPerformed(ActionEvent evt) {
         System.out.println("example: " + evt.paramString());

    Ok, learned a valuable lesson here. As a newbie, I should not have given up so easily, I only spent an hour trying to figure this out.
    I just went back and played with ActionDemo from the Tutorial and it has the same result as I got, cmd: null, when you press a button.
    So, this appears to be expected behaviour.

  • JMenuBar & JMenu

    I have a frame where when it is called I want to show a JMenu. The only way I know how to show a JMenu is by adding it to a JMenuBar and when the user selects the title of the JMenuBar(ie. File) then the attatched JMenu is shown. In my case there is only one selection to be made from the JMemuBar, so I want to bypass that step all together and just show the JMenu in the frame that is presented because the user has no other option but to look at the JMenu(Much like when you right click in Windows) . My question again is how can I just show a JMenu without having to attatch it to a JMenuBar. In other words, I do not want the title bar just the one and only JMenu when the frame is called. How can I accomplish this?
    Ie
    JMenu list = new JMenu("List");
    list contents
    JMenuBar menuBar = new JMenuBar();
    menuBar.add(list);
    I do not want the JMenuBar on my frame nor the included title "List". Though what I do want is the list itself when the frame is called. Again, much like right-clicking in Windows. Can someone please help me with this.

    Hi,
    Check the tutorial on how to use menus. It describes how to create a popup menu (right click on windows brings up a popup menu)
    http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html
    /Kaj

  • JMenuBar hides under Canvas

    Hello,
    I am doing a program that has a JMenuBar and Canvas,
    The Menu Bar shows but when i click on it the JMenuItems are not shown they are hidden under the canvas which is stretched on the frame,
    when i dont add the canas the menu bar works .
    Thanks for help.

    Don't mix AWT (Canvas) and Swing (JMenuBar) unless you have very strong reasons and know what you are doing. Instead of a Canvas, can you use a JPanel? Hopefully all is in a Swing root container such as a JFrame or JApplet.

  • JMenuBar Overlapping

    I have a JDesktop which holds a JMenuBar at the top and various JInternalFrames. But if you drag an internal frame up toward the top of the Desktop, the top of the frame(the part you need to click in order to drag the frame) slides underneath the JMenuBar and you can no longer move that internalFrame. Can I set 'layers' somehow to prevent this or any ideas?

    Add the menuBar to the JFrame using setJMenuBar(...) method.

  • Help JMenuBar conflict with JPanel

    Hi, I'm creating an image converter. The simple Interface is constituted of a JFrame containing a Menu Bar, some buttons, and a central JPanel (or JScrollPanel or Panel that i've tried...) on I display a Tiff Image.
    The problem is:
    When I launch the tool, my menu and my menu Items are visible but when I load an Image, the menu Items that I open from menu are displayed under the Panel.
    Someone can help me please?
    Thanx.

    In you message you mention that you use a Panel. Does this mean that you are using a java.awt.Panel to display your image. If you are than that is your problem as Panel is an old heavywieght gui component whereas you are probably using JMenuBar and JMenuItem for the menu which are lightweight components. You should not mix the two types as you get problems. Make the panel a JPanel and stick to using lightweight swing components and it should be ok.

  • JMenuBar causes setExtendedState(Frame.MAXIMIZED_BOTH) to not work?

    Hello folks,
    I'm trying to teach myself Java Swing and I'm going through the tutorials and trying to create little applications as a learning process.
    Originally, my intent for this little dummy application of mine was for it to load up maximized. After some searching, I found that you can use setExtendedState(Frame.MAXIMIZED_BOTH);
    This worked great until I wanted to create a menu bar. I added JMenuBar and it seems to nullify what I had done before with the setExtendedState.
    The problem seems to be with frame.pack();
    If I remove the pack function then the screen starts out maximized with the menu bar. If I add it in, then the screen loads up fitted, and when minimized, the frames seem to have no width/height.
    Can someone please give me some advice on how I could load up the screen maximized with the menu bar? And when unmaximized (but not minimized), the screen has a reasonable width/height?
    Thanks in advance.

    HI ,
    Actually i also have the same problem, i am creating a gui with four splits . i want the frame to extend to the maximum position when maximize is clicked.
    plz help me out what to use actually to make my screen flexible to minimum nd maximum positions.
    thanks
    swathi.

  • Concept: How is JMenuBar implemented?

    Hi everybody,
    I was working on a subclass of JMenuBar, and I always thought that JMenuBar was implemented like a LinkedList, so you could add a menu at an index, remove a menu at an index, and set a particular menu at an existing index. I was taking a closer look at the Java API, though, and it seems like that isn't the case. Could anyone give me some clarification about how JMenuBar is implemented, please? It doesn't seem to be in the tutorials. It seems like I'll need to combine JMenuBar and LinkedList somehow, but I'd rather not do that unless I'm completely wrong about how JMenuBar is implemented. If anyone could give me some information, I'd really appreciate it.
    Thanks,
    Jezzica85
    Edited by: jezzica85 on Jan 15, 2009 9:07 AM

    Since JMenuBar is a descendant of Container (as are all JComponents), I believe you can use the add(Component c, index i) and remove(int index) methods defined in Container. If you look in JMenuBar.java, add(JMenu) and getMenu(int index) are just covers for the Container methods add() and getComponentAtIndex().

  • JMenuItem.

    Dear all,
    I downloaded the following program from Sun to create JMenuBar and JManuItem but there is no function. I need to display say 'Hello World' when I click one of the JMenuItem. May I know how to write the function in the following context ?
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.JCheckBoxMenuItem;
    import javax.swing.JRadioButtonMenuItem;
    import javax.swing.ButtonGroup;
    import javax.swing.JMenuBar;
    import javax.swing.KeyStroke;
    import javax.swing.ImageIcon;
    import javax.swing.JTextArea;
    import javax.swing.JScrollPane;
    import javax.swing.JFrame;
    * This class exists solely to show you what menus look like.
    * It has no menu-related event handling.
    public class MenuLookDemo extends JFrame {
        JTextArea output;
        JScrollPane scrollPane;
        public MenuLookDemo() {
            JMenuBar menuBar;
            JMenu menu, submenu;
            JMenuItem menuItem;
            JCheckBoxMenuItem cbMenuItem;
            JRadioButtonMenuItem rbMenuItem;
            addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
            //Add regular components to the window, using the default BorderLayout.
            Container contentPane = getContentPane();
            output = new JTextArea(5, 30);
            output.setEditable(false);
            scrollPane = new JScrollPane(output);
            contentPane.add(scrollPane, BorderLayout.CENTER);
            //Create the menu bar.
            menuBar = new JMenuBar();
            setJMenuBar(menuBar);
            //Build the first menu.
            menu = new JMenu("A Menu");
            menu.setMnemonic(KeyEvent.VK_A);
            menu.getAccessibleContext().setAccessibleDescription(
                    "The only menu in this program that has menu items");
            menuBar.add(menu);
            //a group of JMenuItems
            menuItem = new JMenuItem("A text-only menu item",
                                     KeyEvent.VK_T);
            //menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
            menuItem.setAccelerator(KeyStroke.getKeyStroke(
                    KeyEvent.VK_1, ActionEvent.ALT_MASK));
            menuItem.getAccessibleContext().setAccessibleDescription(
                    "This doesn't really do anything");
            menu.add(menuItem);
            menuItem = new JMenuItem("Both text and icon",
                                     new ImageIcon("images/middle.gif"));
            menuItem.setMnemonic(KeyEvent.VK_B);
            menu.add(menuItem);
            menuItem = new JMenuItem(new ImageIcon("images/middle.gif"));
            menuItem.setMnemonic(KeyEvent.VK_D);
            menu.add(menuItem);
            //a group of radio button menu items
            menu.addSeparator();
            ButtonGroup group = new ButtonGroup();
            rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
            rbMenuItem.setSelected(true);
            rbMenuItem.setMnemonic(KeyEvent.VK_R);
            group.add(rbMenuItem);
            menu.add(rbMenuItem);
            rbMenuItem = new JRadioButtonMenuItem("Another one");
            rbMenuItem.setMnemonic(KeyEvent.VK_O);
            group.add(rbMenuItem);
            menu.add(rbMenuItem);
            //a group of check box menu items
            menu.addSeparator();
            cbMenuItem = new JCheckBoxMenuItem("A check box menu item");
            cbMenuItem.setMnemonic(KeyEvent.VK_C);
            menu.add(cbMenuItem);
            cbMenuItem = new JCheckBoxMenuItem("Another one");
            cbMenuItem.setMnemonic(KeyEvent.VK_H);
            menu.add(cbMenuItem);
            //a submenu
            menu.addSeparator();
            submenu = new JMenu("A submenu");
            submenu.setMnemonic(KeyEvent.VK_S);
            menuItem = new JMenuItem("An item in the submenu");
            menuItem.setAccelerator(KeyStroke.getKeyStroke(
                    KeyEvent.VK_2, ActionEvent.ALT_MASK));
            submenu.add(menuItem);
            menuItem = new JMenuItem("Another item");
            submenu.add(menuItem);
            menu.add(submenu);
            //Build second menu in the menu bar.
            menu = new JMenu("Another Menu");
            menu.setMnemonic(KeyEvent.VK_N);
            menu.getAccessibleContext().setAccessibleDescription(
                    "This menu does nothing");
            menuBar.add(menu);
        public static void main(String[] args) {
            MenuLookDemo window = new MenuLookDemo();
            window.setTitle("MenuLookDemo");
            window.setSize(450, 260);
            window.setVisible(true);
    }

    In the same tutorial there is a section titled "Handling Events From Menu Items" which includes sample code:
    http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html#event

Maybe you are looking for

  • GeForce2 GTS refresh rate problem

    I have a GeForce2 GTS (MSI Starforce 815) running on an IWill KK266 (KT133A chipset) and a Sony GDM-400PS monitor. My problem is that when I set the refresh rate to "optimal" it runs at 60hz. This is a problem because my eyes are really sensitive to

  • How can i get my iMessage on my mac to sync with the texts i get on my iPhone?

    how can i get my iMessage on my mac to sync with the texts i get on my iPhone?

  • How to prepare for SAP CRM

    Hi friends,        I'm new to SAP CRM. Can any one tell me where do i get related materials and how to prepare and related sites to find materials and CV.         Rewarded with points. Thanks&Regards, Anil

  • Variables don't change at the first time

    i often work with variables. sometimes i read from a varibale at the beginning of the aplication, and save from a variable at the end of the appplication. the problem is that the values aren't right at the first time.the values only changes at the se

  • Driver update issues?: wireless LAN no longer works

    Hi, I'm using an X200 (7458-CTO, XP SP3). For a week (or a couple?) I've been unable to use Access Connections (AC) to get my wireless to work. When I uninstall-reinstall the driver (for the 5300 AGN) I can get Windows to do it for me -- but I have n