0

Hello fellow coders of the night, I am stuck with a moral dilemma (well not moral, but mostly i don't know what to do). Suppose I have one button that can do several actions, depending on the menu item which is chosen. Basically, I've imagined this

private void menuButtonActionPerformed(ActionEvent b)
ActionEvent a 
if(a.getSource()==menuItem)
  if(b.getSource()==button)
    do this and that

Is this the correct way to do this? because if it is I'd have to add ActionListeners on the menuItem but I get stuck with some stupid error code somewhere!

Thanks in advance for helping me!

Post Scriptum : @David, I've tried this, however the initial condition isn't verified.

   private void buttonValidateActionPerformed(java.awt.event.ActionEvent evt)
   ActionListener l = (ActionEvent e) -> {
     if(e.getSource()==menuItemAdd)
     {
         System.out.println("eureka!");
         buttonSearch.setEnabled(false);
      if (evt.getSource()==buttonValidate)
        {

        DataTransac dt = new DataTransac();

        dt.addCoders("...");
        }
     }
     if(e.getSource()==itemDelete)
     {
        DataTransac dt = new DataTransac();
        dt.deleteCoders("...");
     }

  };

  menuItemAdd.addActionListener(l);
  itemDelete.addActionListener(l);
Fares
  • 893
  • 1
  • 11
  • 24

1 Answers1

0

That won't work; your listener will get a different invocation for each time the listener is used -- so the event source will be either a button or a menu item for a single invocation.

You'll need to respond to the menu item with one ActionListener that stores state, and then separately handle the button action. You could do this with one listener, but I wouldn't; I'd do this:

private MenuItem selected;

private class MenuItemListener implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        // if you really want to have one listener for multiple menu items,
        // continue with the .getSource() strategy above, but store some
        // state outside the listener
        selected = (MenuItem)event.getSource();

        // you could alternatively have a different listener for each item
        // that manipulates some state
    }
}

private class ButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        // take conditional action based on selected menu item, as you describe
        // in the question
    }
}

void setup() {
    JMenuItem first = /* ... */;
    JMenuItem second = /* ... */;

    MenuItemListener listener = new MenuItemListener();
    first.addActionListener(listener);
    second.addActionListener(listener);

    JButton button = /* ... */;
    button.addActionListener(buttonListener);
}

Generally speaking this is the preferred approach -- use a different listener for each semantic action, rather than one that introspects the source. Your code will be cleaner, simpler, and easier to understand.

For the same reasons, some people prefer to use anonymous classes for Java event listeners. Here's a Gist that shows several syntaxes: https://gist.github.com/sfcgeorge/83027af0338c7c34adf8. I personally prefer, if you are on Java 8 or higher:

button.addActionListener( event -> {
    // handle the button event
} );
David P. Caldwell
  • 3,394
  • 1
  • 19
  • 32