2

I'm implementing an application using MVC pattern, In some cases I have created model and controller from the view, and called the appropriate method of the controller inside the view. Is this a design issue, if so what could be the solution, I can't crate all my models in the main because some of them are invoked by a click from the GUI.

here is an example code:

public class MyView extends JInternalFrame{

private JComboBox<String> myList;
private JButton button;

public MyView(){
    super("MyView", true, // resizable
            false, // closable
            false, // maximizable
            true);// iconifiable
    setSize(250, 150);

    setLocation(300,300);

    myList= new JComboBox<String>();

    button= new JButton("Click");
    button.addActionListener(new Listener());

    JLabel label = new JLabel("Example");

    Border lineBorder = BorderFactory.createTitledBorder("Example UI");
    JPanel panel= new JPanel();
    panel.setBorder(lineBorder);
    panel.setLayout(new GridLayout(3, 1));
    panel.add(label);
    panel.add(list);
    panel.add(button);

    add(panel);
}

class Listener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        String button = actionEvent.getActionCommand();

        if(button==button.getText()){
            Model model = new Model ();
            Controller controller = new Controller (model, MyView.this);
            controller.doSomething();
        }

    }
}
PRCube
  • 566
  • 2
  • 6
  • 19
  • Have you googled? Check this out for example: http://stackoverflow.com/questions/5217611/the-mvc-pattern-and-swing – Jack Flamp Apr 05 '16 at 19:12
  • 2
    I did more than a basic google search, read many books but they don't cover specific solution to a such problem. According to the link you've provided, what I have done is fine. I don't have an issue understanding the MVC pattern, I'm trying to find out if what I have done is an appropriate/conventional design approach. – PRCube Apr 05 '16 at 19:22
  • More on this topic [here](http://stackoverflow.com/a/3072979/230513) and [here](http://stackoverflow.com/a/25556585/230513). – trashgod Apr 06 '16 at 02:26
  • 1
    @trashgod Thank you, I already have looked into those links, it is a general overview of MVC. I'm not looking for that. I just need confirmation of whether what I have done above is correct or wrong in terms of software design. – PRCube Apr 06 '16 at 13:20
  • Absent a complete example, it's hard to comment; I generally use [`Action`](https://docs.oracle.com/javase/tutorial/uiswing/misc/action.html) to encapsulate functionality. – trashgod Apr 06 '16 at 16:39

1 Answers1

1

After a week of research, I have concluded that the way I have done is fine. User interacts with view which interacts with controller. There is no convention where and how to create models and controllers. It all depends on the specific problem pattern is used for. As long as main principles of MVC is applied, you're free.

PRCube
  • 566
  • 2
  • 6
  • 19