0

After reading some posts, I am not sure about the difference between these two ways to implement ActionListeners. Is there any good reason to code in one way or another? What about view-decoupling?

OPTION 1:

Implement getButton()/setButton() methods in the view, and use these methods to add ActionListeners in the controller.

VIEW

public JButton getBtnRun() {
        return btnRun;
    }

CONTROLLER

m_view.getBtnRun().addActionListener(new ButtonListener());

OPTION 2:

Implement methods in the view, and call these methods from the controller, like here

VIEW

public void setOpenFileAction(Action action) {
      displayText.setOpenFileButtonAction(action);
      fileMenu.add(new JMenuItem(action));
   }

CONTROLLER

view.setOpenFileAction(new OpenFileAction(view, model, "Open File",
            KeyEvent.VK_O));
Community
  • 1
  • 1
capovawi
  • 377
  • 8
  • 21

1 Answers1

3
  • IMHO It's better to have just one Swing Action.

  • If you e.g. enable/disable Swing Action it should enable/disable the JMenu and JButton as well.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
StanislavL
  • 56,971
  • 9
  • 68
  • 98