red_black_guy
red_black_guy

Reputation: 119

ActionListener not responding [example]

I have ActionListener class implemented as parameter and isn't responding.

I'm using MVC pattern and calling controllers within other controllers. I have created simplified version of my program (which still isn't working).

I have one frame with one button which when pressed prints in console.

public class MainFrame extends JFrame {

    private JButton button = new JButton("Print");

    public MainFrame() {
        setSize(200, 100);

        this.add(button);

        setLocationRelativeTo(null);
        this.setDefaultCloseOperation(MainFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public void validateButton(ActionListener e) {
        this.button.addActionListener(e);
    }
}

Here is the action

public class ValidateAction implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("It works!");
    }

}

I call this action inside of EditController

public class EditController {

    private MainFrame mf = new MainFrame();

    public EditController(MainFrame mf) {
        this.mf = mf;

        this.mf.validateButton(new ValidateAction());
    }

}

I call this EditController inside of MenuController

public class MenuController {

    public MenuController(MainFrame mf) {

        this.makeEditController(mf);
    }

    public void makeEditController(MainFrame mf) {
        new EditController(mf);
    }

}

And I call MenuController inside of main Controller

public class Controller {

    public Controller(MainFrame mf) {

        this.makeMenuController(mf);
    }

    public void makeMenuController(MainFrame mf) {
        new MenuController(mf);
    }

}

Finally I call everything in main class

public class Main {

    public static void main(String[] args) {
        MainFrame mf = new MainFrame();
        new Controller(mf);
    }

}

And it's not working...

Upvotes: 0

Views: 36

Answers (1)

c0der
c0der

Reputation: 18792

You are creating MainFrame twice. See comment:

class EditController {

    private MainFrame mf; //= new MainFrame();

    public EditController(MainFrame mf) {
        this.mf = mf;
        this.mf.validateButton(new ValidateAction());
    }
}

Upvotes: 1

Related Questions