简体   繁体   中英

JFrame Action Listener that listens to all menu items?

So I have a JFrame set up with a menu with the current structure that looks something along the lines of this:

File
  Exit
Pages
  Reviews
    A
    B
    C
Help
  About

I want to create a Action Listener that only listens to menu items under Reviews. Is this a possibility (and if so, how) or do I have to create a generic listener and check if it's one of those items?

Yes, it is possible:

  • Store your menu items as fields
  • Add the same ActionListener to each menu item.
  • In the listener check for the source to know which item was clicked.

Should look like:

public class YourFrame extends JFrame implements ActionListener {

   private final JMenuItem menuA, menuB;

   public YourFrame(){
      super("Your app");
      JMenuBar menuBar = new JMenuBar();
      JMenu menuReviews = new JMenu("Reviews");
      menuA = new JMenuItem("A");
      menuB = new JMenuItem("B");
      ...
      menuReviews.add(menuA);
      menuReviews.add(menuB);
      menuBar.add(menuReviews);
      setJMenuBar(menuBar);
      ...
      menuA.addActionListener(this);
      menuB.addActionListener(this);
      ...
   }

   public void actionPerformed(ActionEvent event){
      if(event.getSource()==menuA){
         System.out.println("Menu A clicked");
         ...
      }else if(event.getSource()==menuB){
         System.out.println("Menu B clicked");
         ...
      }
   }

}

Note that here I let the JFrame implement ActionListener , but this is just for convenience. You could use a dedicated class, or an anonymous class created in the constructor:

ActionListener reviewsListener = new ActionListener(){
   public void actionPerformed(ActionEvent event){
      if(event.getSource()==menuA){
         System.out.println("Menu A clicked");
         ...
      }else if(event.getSource()==menuB){
         System.out.println("Menu B clicked");
         ...
      }
   }
};
menuA.addActionListener(reviewsListener);
menuB.addActionListener(reviewsListener);

If you want to integrate this process a little more, I could also suggest to extend JMenu , so that you can pass it your action listener and add it systematically to new menu items.

public class YourJMenu extends JMenu {
   private ActionListener listener;
   public YourJMenu(String name, ActionListener listener){
      super(name);
      this.listener = listener;
   }
   @Override
   public JMenuItem add(JMenuItem item){
      item.addActionListener(listener);
      return super.add(item);
   }
}

With this, you just need to write:

JMenu menuReviews = new YourJMenu("Reviews", this);

and drop the:

menuA.addActionListener(this);
menuB.addActionListener(this);

Using a common method we can add the action listener to all the menu items under a menu. Below is a example code.

public class MenuItemEvent {
    JFrame objFrm = new JFrame("Menu event demo");
    JMenuBar mBar;
    JMenu mnu;
    JMenuItem mnuItem1, mnuItem2, mnuItem3;
    public void show() {
        objFrm.setSize(300, 300);
        mBar = new JMenuBar();
        mnu = new JMenu("Reviews");
        mBar.add(mnu);

        mnuItem1 = new JMenuItem("A");
        mnu.add(mnuItem1);

        mnuItem2 = new JMenuItem("B");
        mnu.add(mnuItem2);

        mnuItem3 = new JMenuItem("C");
        mnu.add(mnuItem3);

        //method call
        fnAddActionListener(mnu);

        objFrm.setJMenuBar(mBar);
        objFrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        objFrm.setVisible(true);
    }

    //method to add action listener to all menu items under a menu
    public void fnAddActionListener(JMenu mnu) {
        if (mnu.getItemCount() != 0) {
            for (int iCount = 0; iCount < mnu.getItemCount(); iCount++) {
                (mnu.getItem(iCount)).addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        fnMenuItemAction(e);
                    }
                });
            }
        }
    }

    //menu item action event
    public void fnMenuItemAction(ActionEvent e) {
        if (e.getSource().equals(mnuItem1)) {
            System.out.println("Menu Item 1");
        } else if (e.getSource().equals(mnuItem2)) {
            System.out.println("Menu Item 2");
        } else if (e.getSource().equals(mnuItem3)) {
            System.out.println("Menu Item 3");
        }
    }

    public static void main(String[] args) {
        new MenuItemEvent().show();
    }
}

or with the below function

//fnMenuItemAdd(mnu,mnuItem1)
//etc.

public void fnMenuItemAdd(JMenu mnu, JMenuItem mni) {
    mnu.add(mni);
    mni.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            fnMenuItemAction(e);
        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM