Search code examples
javaclassmenuactionlistenermenubar

How can I utilize a secondary class to handle my events?


I've created two separate classes whereas I have instantiated my menu bar, and a second class to handle the events; since I do have a lot of options on the menu bar I'd like to handle.

I have the menu bar and it's structure all set up, now the next step is to handle the events when the user clicks an option on the menu bar.

Here's a snippet of two items from my main menu-bar class:

    JMenuItem addOrangeItem = new JMenuItem("Orange");
    addOrangeItem.addActionListener(new MenuActionListener().orangeActionPerformed(e));

    JMenuItem addAppleItem = new JMenuItem("Apple");
    addAppleItem.addActionListener(new MenuActionListener().appleActionPerformed(e));

Here's my event handling class:

public class MenuActionListener implements ActionListener {

    public void orangeActionPerformed(ActionEvent e) {
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("I have chosen an orange!");
    }


    public void appleActionPerformed(ActionEvent e) {
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("I have chosen an apple!");
    }
}

The problem lies on this line of code in my main menu-bar class:

addAppleItem.addActionListener(new MenuActionListener().appleActionPerformed(e)); whereas the e for my ActionEvent is underlined as red and I'm not sure what to do to get it working.

The goal of my code is to select the Apple/orange item/s and my event handling class will then return some code.

My question is how can I edit the above line of code so that I can then handle my menu bar items correctly?

If you require any more information, please let me know and I will get right onto that.

Any help is greatly appreciated, thank you.


Solution

  • This is an invalid syntax :addActionListener(new MenuActionListener().orangeActionPerformed(e)) .

    addActionListener wants an ActionListener object, not void (which is the result of new MenuActionListener().orangeActionPerformed(e)), and also e is an unknown variable here.

    This will work : addActionListener(new MenuActionListener()) , but since you need different actions depending on the pressed item, you may use the action command system :

    Set an action command on your JMenuItem (also note that one instance of ActionListener is enough for both buttons) :

    ActionListener listener = new MenuActionListener();
    
    JMenuItem addOrangeItem = new JMenuItem("Orange");
    addOrangeItem.setActionCommand("orange");// set action command
    addOrangeItem.addActionListener(listener);
    
    JMenuItem addAppleItem = new JMenuItem("Apple");
    addAppleItem.setActionCommand("apple");// set action command
    addAppleItem.addActionListener(listener);
    

    Then retrieve the action command in the listener (in actionPerformed), and decide what to do :

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
        public class MenuActionListener implements ActionListener {
    
            public void orangeActionPerformed() {
                System.out.println("I have chosen an orange!");
            }
    
            public void appleActionPerformed() {
    
                System.out.println("I have chosen an apple!");
    
            }
    
            @Override
            public void actionPerformed(final ActionEvent e) {
    
                String command = e.getActionCommand();
    
                switch (command) {
    
                case "orange":
                    orangeActionPerformed();
                    break;
                case "apple":
                    appleActionPerformed();
                    break;
                default:
    
                }
    
            }
        }
    

    Note that setActionCommand is a method from AbstractButton, and also works for JButton for instance.