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

    }
}
5
  • Have you googled? Check this out for example: stackoverflow.com/questions/5217611/the-mvc-pattern-and-swing Commented Apr 5, 2016 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. Commented Apr 5, 2016 at 19:22
  • More on this topic here and here. Commented Apr 6, 2016 at 2: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. Commented Apr 6, 2016 at 13:20
  • Absent a complete example, it's hard to comment; I generally use Action to encapsulate functionality. Commented Apr 6, 2016 at 16:39

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

have doubts as to whether this answer is entirely correct. But it's clearly a comforting answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.