I'm trying to make a GUI to generate characters for a tabletop RPG, using MVC coding practices. I will have several "control panel" classes, each with a particular set of buttons to handle the different aspects of the character creation process (rolling attributes, buying equipment, etc). My Controller class will have as members an instance of each "control panel" class. My generalized code is here:
public class Controller{
private Model m; //class Model not shown
private Viewer v; //class Viewer not shown
private JFrame frame;
private ControlPanelOne cpo;
private ControlPanelTwo cpt; //class ControlPanelTwo not shown
public Controller(){
m = new Model();
v = new Viewer();
frame = new JFrame();
frame.setLayout(…);
cpo = new ControlPanelOne();
cpt = new ControlPanelTwo();
frame.add(cpo.getPanel());
frame.add(cpt.getPanel());}
public void update(){
m.update();
v.update();}}
public class ControlPanelOne{
private JPanel panel;
private JButton button;
public ControlPanelOne(){
panel = new JPanel();
button = new JButton(“press me”);
panel.add(button);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//do some stuff;
//call Controller’s update method;}})} //the important bit!
public JPanel getPanel(){
return panel;}}
public class Tester{
public static void main(String[] args){
Controller c = new Controller();}}
How can I get ControlPanelOne to see and call on Controller’s update() method when ControlPanelOne’s button is pressed?
I tried making ControlPanelOne extend Controller so I could call super.update() when ControlPanelOne’s button is pressed, but then when a Controller is instantiated (such as in the Tester class), its ControlPanelOne member is also instantiated, whereby both Controller and ControlPanelOne’s constructors are called in an infinite loop of construction.
Trying to trick the compiler by making Controller’s update() method static doesn’t work because instances m and v aren’t static.
I don’t want to make ControlPanelOne an inner class of Controller if I can help it. There will be other “control panel” classes (ControlPanelTwo, etc.), and I’m afraid making them all inner classes would make Controller’s code long and messy. I would like each “control panel” as a separate class to handle one particular function for neatness’s sake, and for ease of maintenance and modular code reuse.
It seems clear that ControlPanelOne shouldn’t have a Controller as a member variable, because a) it’s conceptually backwards and b) instances of the other “control panels” would need their own instances of a Controller…
I used to have Controller extend JFrame and ControlPanelOne extend JPanel (so that Controller could add() an instance of ContollPanelOne directly instead for calling ControlPanelOne’s getPanel() method), but I read making classes extend such components instead of making such components members of your class is poor form. But if it would somehow help to go back to having Controller and ControlPanelOne extend JFrame and JPanel…
Perhaps my idea of MVC is wrong...
Please advise