Member Avatar for mehnihma

Can you help me with one problem,

I dont know how to invoke action op menu not the menu item?

I have File and Help menus and I need to invoke action when the Help menu is pressed but dont understand how, I know how to do in on MenuItem but same thing does not work on menu?

// menu 2

     menu2 = new JMenu("Help");
     menu2.setMnemonic('H');
     menuBar.add(menu2);     

menu2.addActionListener(this);

public void actionPerformed(ActionEvent e)
  {
      if (e.getActionCommand().equals("Exit"))
      {
         System.exit(0);
      }
      else if (e.getSource() == menu2) {
         JOptionPane.showMessageDialog(frame, "Protected Viewer\n Version 2.1\n January 15, 2011\n 218", "About Protected View", JOptionPane.INFORMATION_MESSAGE);           
         }

if i do this for menu item like Exit it works but in menu it does not work?

JMenu isn't designated to invoke any action, JMenu is container nested JMenuItem(s), sure there are two/three choices how to determine if JMenu is visible / hidden

my question Why do you want/need to listening for that, for why reason

Member Avatar for mehnihma

That is asked in the lab that I need to do

that would be big mistake in the lab, only too higher/est applications needed listening for that and in other hands talking about wrong desing

there are some issue with Focus, ButtonModel and another for_your_lab_non_important ZOO, possible listeners are

- MenuListener

- FocusListener

- Property/ChangeListener

for me every a.m. returns same results

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class ActionExample {

    public static void main(String[] args) {
        Action sample = new SampleAction();
        JMenu menu = new JMenu("Menu");
        menu.setMnemonic(KeyEvent.VK_M);
        menu.add(sample);
        menu.addMenuListener(new SampleMenuListener());
        JToolBar tb = new JToolBar();
        tb.add(sample);
        JTextField field = new JTextField(10);
        field.setAction(sample);
        JFrame f = new JFrame("ActionExample");
        JMenuBar mb = new JMenuBar();
        mb.add(menu);
        f.setJMenuBar(mb);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(tb, BorderLayout.NORTH);
        f.getContentPane().add(field, BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private ActionExample() {
    }
}

class SampleMenuListener implements MenuListener {

    @Override
    public void menuSelected(MenuEvent e) {
        System.out.println("menuSelected");
    }

    @Override
    public void menuDeselected(MenuEvent e) {
        System.out.println("menuDeselected");
    }

    @Override
    public void menuCanceled(MenuEvent e) {
        System.out.println("menuCanceled");
    }
}

class SampleAction extends AbstractAction {

    private static final long serialVersionUID = 1L;

    public SampleAction() {
        super("Sample");
        putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("alt S"));
        putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_S));
        putValue(SHORT_DESCRIPTION, "Just a sample action");
    }

    @Override
    public void actionPerformed(ActionEvent evt) {
        System.out.println("sample...");
    }
}

That is asked in the lab that I need to do

check this out not the best but its something, as mKorbel said and i agree java never intended a menu to be used like that, and its not a good question at all for the lecturers to ask but here:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package menu;

import javax.swing.*;
import java.awt.event.*;

public class Menu extends JFrame {

    public Menu() {
        super("Menu example");

        final JMenu file = new JMenu("File");
        file.setMnemonic('F');
        JMenuItem newItem = new JMenuItem("New");
        newItem.setMnemonic('N');
        file.add(newItem);
        JMenuItem openItem = new JMenuItem("Open");
        openItem.setMnemonic('O');
        file.add(openItem);
        JMenuItem exitItem = new JMenuItem("Exit");
        exitItem.setMnemonic('x');
        file.add(exitItem);
        //here i add an item listner to File/file
        file.addItemListener(new ItemListener() {

            int count = 0;

            @Override
            public void itemStateChanged(ItemEvent e) {
                if (count == 2) {
                    count = 0;
                }
                if (count != 1) {
                    if (isActive()) {
                        System.out.println("File is pressed");
                    }
                }
                count++;
            }
        });
        //adding action listener to menu items
        newItem.addActionListener(
                new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("New is pressed");
                    }
                });
        openItem.addActionListener(
                new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("Open is pressed");
                    }
                });
        exitItem.addActionListener(
                new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("Exit is pressed");
                    }
                });
        JMenuBar bar = new JMenuBar();
        setJMenuBar(bar);
        bar.add(file);

        getContentPane();
        setSize(200, 200);
        setVisible(true);
    }

    public static void main(String[] args) {
        Menu app = new Menu();
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
Member Avatar for mehnihma

but how to do it with

public void menuSelected(MenuEvent e) {
         System.out.println("menuSelected");
      }

in a separate class?

but how to do it with

public void menuSelected(MenuEvent e) {
         System.out.println("menuSelected");
      }

in a separate class?

is it not already in a separate class?:

class SampleMenuListener implements MenuListener {
 
    @Override
    public void menuSelected(MenuEvent e) {
        System.out.println("menuSelected");
    }
 
    @Override
    public void menuDeselected(MenuEvent e) {
        System.out.println("menuDeselected");
    }
 
    @Override
    public void menuCanceled(MenuEvent e) {
        System.out.println("menuCanceled");
    }
}

only difference is its in a nested class... so just add public class SampleMenuListener implements... and you can put it in a separate class in the package

Member Avatar for mehnihma

i have a class helpmenulistiner

public class help implements ActionListener implements MenuListener

how can I do that?

i have a class helpmenulistiner

public class help implements ActionListener implements MenuListener

how can I do that?

public class help implements ActionListener, MenuListener

Member Avatar for mehnihma

i get this error

HelpListener.java:19: cannot find symbol
symbol: class MenuListener
public class HelpListener implements ActionListener, MenuListener {

Member Avatar for mehnihma
import java.awt.Component;
import java.awt.event.*;
import java.awt.event.ActionListener;
import javax.swing.*;

/**
 * The listener interface for receiving help events.
 *
 * @see HelpEvent
 */
   public class HelpListener implements ActionListener, MenuListener   {

   /** The exit2 and about menu Items. */
      private JMenuItem about2, exit2;

   /** The frame. */
      private JFrame frame;

   /**
    * default constructor for help button.
    *
    * @param about2 the about button
    * @param exit2 the exit button
    */
      public HelpListener(JMenuItem about2, JMenuItem exit2) {

         this.about2 = about2;
         this.exit2 = exit2;
      }

   /**
    * indicates that a meaningful action occurred.
    *
    * @param e the e
    * @e Action Listener action when button pressed
    */
      @Override
      public void actionPerformed(ActionEvent e) {

         if (e.getActionCommand().equals("Exit"))
         {
            System.exit(0);
         }

         else if (e.getActionCommand().equals("About")) {
            JOptionPane.showMessageDialog(frame,"Protected Text Viewer\n Version 2.1\n February 5, 2011\n M 218", "About Protected View", JOptionPane.INFORMATION_MESSAGE);
            System.out.println("M");
         }
      }

      @Override
      public void menuSelected(MenuEvent e) {
         System.out.println("menuSelected");
      }

      @Override
      public void menuDeselected(MenuEvent e) {
         System.out.println("menuDeselected");
      }

      @Override
      public void menuCanceled(MenuEvent e) {
         System.out.println("menuCanceled");
      }
   }// end class

Import these 2:

import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.