0

I want to write an application in Java SWING using the MVC architecture, but I have problem with using multiple controllers because I don't know how to do this.

public class Application {

public static void main(String[] args) {
    Application app = new Application();
    app.Start();

}
public void Start()
{

    MainModel model = new MainModel();
    MainView view = new MainView();
    MainController controller = new MainController(view,model);
    view.setVisible(true);
}}

MainController:

public class MainController {

private MainView theView;
private MainModel theModel;

public MainController(MainView theView, MainModel theModel)
{
    this.theModel = theModel;
    this.theView = theView;

}}

MainView:

public class MainView extends JFrame{
private JMenu menu = new JMenu();

public MainView()
{
    JPanel panel = new JPanel();

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(800,800);
    panel.add(menu);

    this.add(panel);
}}

I do not put here because the model is now empty.

Okay, so I don't know how to use multiple controllers. For example: I want to create new controller UserController and I don't know how to implement this. Should I create new controller in MainController? If I create UserController in MainController how to use it?

Paul Floyd
  • 5,530
  • 5
  • 29
  • 43
azxcqqq
  • 33
  • 5
  • 1
    [*Correctly implementing the MVC pattern in GUI development using Swing in Java*](http://stackoverflow.com/q/25502552/230513) suggests that "not every interaction needs to pass through your application's controller." – trashgod Aug 01 '16 at 12:12

1 Answers1

0

Just create UserController in your Application class where you added your MainController.

Example: if you have 2 buttons you can add actionlistener.

Button1 will do UserController.doSomething();

Button2 will do MainController.doSomeOtherThings();

Thats the way you use it ;)

beeb
  • 1,615
  • 1
  • 12
  • 24