capovawi
capovawi

Reputation: 375

Best way to add ActionListeners to the View

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));

Upvotes: 0

Views: 82

Answers (1)

StanislavL
StanislavL

Reputation: 57401

  • 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.

Upvotes: 3

Related Questions