Reputation: 586
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();
}
}
}
Upvotes: 2
Views: 678
Reputation: 586
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.
Upvotes: 1