I'm trying to get my head around the MVC design pattern using Swing in Java it's pretty confusing at the moment because I don't fully understand it. I have tried to make what I understand of it in eclipse but I have a error on line 68, I'm not sure why everytime I push a button I get a lot of errors being spat out and not the incrementing value of counter. Thanks
public class Model
{
int counter = 0;
int counter()
{
this.counter++;
return this.counter;
}
}
#################################################################################
public class Controller
{
Model mRef;
View vRef;
public Controller(Model m, View v)
{
this.mRef = m;
this.vRef = v;
}
int inc()
{
mRef = new Model();
return mRef.counter();
}
}
######################################################################################
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class View extends JFrame
{
JPanel jp;
JButton jb1;
JLabel jl1;
GridBagConstraints c;
Controller con;
public View()
{
c = new GridBagConstraints();
jp = new JPanel();
jb1 = new JButton("First");
jl1 = new JLabel("Label");
jp.setLayout(new GridBagLayout());
add(jp);
c.gridx = 0;
c.gridy = 0;
jp.add(jb1, c);
jb1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println(con.inc());
}
});
c.gridx = 0;
c.gridy = 2;
jp.add(jl1, c);
setVisible(true);
pack();
}
}