Results 1 to 3 of 3
  1. #1
    equal is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default MVC - have 2 Views but only 1 updates

    Hi I'm doing a TicTacToe-game using MVC-pattern (Model/View/Controller).

    When I make 2 instances of the View class and i press either view1 or view2 only view1 updates and nothing happens to view2.

    Take a look at this picture, here I've pressed the window to the right, but only the left one updates properly: http://img194.imageshack.us/img194/3268/img0002zef.jpg

    I'll attach the 3 classes, since both windows listens to the actionCommand there must be something wrong with the views update?

    Classes:

    View: [Java] MVCView - Pastebin.com
    Model: [Java] MVCModel - Pastebin.com
    Controller: [Java] MVCController - Pastebin.com

    I would really appreciate if anyone could take the time and look thru my code and help me find a solution to this, been stuck here for soon 2 days.

    Thanks in advance!
    Last edited by equal; 05-20-2011 at 10:07 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Please learn to use arrays. This will simplify your code immensely and also make it much easier to debug. Also, you'll want to post your code here to make it easier for us to view and review.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    This is your current code (with slight modification so that all code is in one class):

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Observable;
    import java.util.Observer;
    
    import javax.swing.*;
    
    public class McvGame {
        public static void main(String[] args) {
            GameModel model = new GameModel();
            GameController controller = new GameController(model);
            GameView view = new GameView(controller, model);
            GameView view2 = new GameView(controller, model);
    
            model.addObserver(view2);
            model.addObserver(view);
        }
    }
    
    class GameView extends JFrame implements Observer {
        private GameModel model;
        private GameController controller;
    
        private JPanel gridPanel = new JPanel();
    
        private GridLayout gridLayout = new GridLayout(3, 3);
    
        JButton btn0;
        JButton btn1;
        JButton btn2;
        JButton btn3;
        JButton btn4;
        JButton btn5;
        JButton btn6;
        JButton btn7;
        JButton btn8;
        JButton btn9;
    
        public GameView(GameController aController, GameModel aModel) {
            controller = aController;
            model = aModel;
    
            this.setTitle("TicTacToe");
            this.setSize(400, 400);
            this.setLayout(new BorderLayout());
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    
            createGameButtons();
            setupGridPanel();
    
            this.setVisible(true);
    
        }
    
        private void createGameButtons() {
            btn0 = new JButton();
            btn1 = new JButton();
            btn2 = new JButton();
            btn3 = new JButton();
            btn4 = new JButton();
            btn5 = new JButton();
            btn6 = new JButton();
            btn7 = new JButton();
            btn8 = new JButton();
            btn9 = new JButton("Nytt Spel");
    
            btn0.setActionCommand("btn1");
            btn1.setActionCommand("btn2");
            btn2.setActionCommand("btn3");
            btn3.setActionCommand("btn4");
            btn4.setActionCommand("btn5");
            btn5.setActionCommand("btn6");
            btn6.setActionCommand("btn7");
            btn7.setActionCommand("btn8");
            btn8.setActionCommand("btn9");
            btn9.setActionCommand("nytt");
    
            btn0.addActionListener(controller);
            btn1.addActionListener(controller);
            btn2.addActionListener(controller);
            btn3.addActionListener(controller);
            btn4.addActionListener(controller);
            btn5.addActionListener(controller);
            btn6.addActionListener(controller);
            btn7.addActionListener(controller);
            btn8.addActionListener(controller);
            btn9.addActionListener(controller);
    
        }
    
        private void setupGridPanel() {
            gridPanel.setLayout(gridLayout);
    
            gridPanel.add(btn0);
            gridPanel.add(btn1);
            gridPanel.add(btn2);
            gridPanel.add(btn3);
            gridPanel.add(btn4);
            gridPanel.add(btn5);
            gridPanel.add(btn6);
            gridPanel.add(btn7);
            gridPanel.add(btn8);
            gridPanel.add(btn9);
    
            this.add(gridPanel, BorderLayout.CENTER);
            this.add(btn9, BorderLayout.NORTH);
        }
    
        private void updateView() {
            if (model.btn1State() == false) {
                btn0.setEnabled(false);
                btn0.setText("1");
                model.btn1ChangeTrue();
    
            }
    
            else if (model.btn2State() == false) {
                btn1.setEnabled(false);
                btn1.setText("2");
                model.btn2ChangeTrue();
    
            }
    
            else if (model.btn3State() == false) {
                btn2.setEnabled(false);
                btn2.setText("3");
                model.btn3ChangeTrue();
            } else if (model.btn4State() == false) {
                btn3.setEnabled(false);
                btn3.setText("4");
                model.btn4ChangeTrue();
    
            }
    
            else if (model.btn5State() == false) {
                btn4.setEnabled(false);
                btn4.setText("5");
                model.btn5ChangeTrue();
    
            }
    
            else if (model.btn6State() == false) {
                btn5.setEnabled(false);
                btn5.setText("6");
                model.btn6ChangeTrue();
    
            }
    
            else if (model.btn7State() == false) {
                btn6.setEnabled(false);
                btn6.setText("7");
                model.btn7ChangeTrue();
    
            }
    
            else if (model.btn8State() == false) {
                btn7.setEnabled(false);
                btn7.setText("8");
                model.btn8ChangeTrue();
    
            }
    
            else if (model.btn9State() == false) {
                btn8.setEnabled(false);
                btn8.setText("9");
                model.btn9ChangeTrue();
    
            }
        }
    
        public void update(Observable model, Object arg) {
            GameModel aModel = (GameModel) model;
            updateView();
    
        }
    
    }
    
    class GameModel extends Observable {
        private boolean btn1 = true;
        private boolean btn2 = true;
        private boolean btn3 = true;
        private boolean btn4 = true;
        private boolean btn5 = true;
        private boolean btn6 = true;
        private boolean btn7 = true;
        private boolean btn8 = true;
        private boolean btn9 = true;
    
        public void btn1ChangeFalse() {
            btn1 = false;
            setChanged();
            notifyObservers();
    
        }
    
        public void btn2ChangeFalse() {
            btn2 = false;
    
            setChanged();
            notifyObservers();
        }
    
        public void btn3ChangeFalse() {
            btn3 = false;
    
            setChanged();
            notifyObservers();
    
        }
    
        public void btn4ChangeFalse() {
    
            btn4 = false;
    
            setChanged();
            notifyObservers();
        }
    
        public void btn5ChangeFalse() {
    
            btn5 = false;
    
            setChanged();
            notifyObservers();
        }
    
        public void btn6ChangeFalse() {
    
            btn6 = false;
    
            setChanged();
            notifyObservers();
        }
    
        public void btn7ChangeFalse() {
    
            btn7 = false;
    
            setChanged();
            notifyObservers();
        }
    
        public void btn8ChangeFalse() {
    
            btn8 = false;
    
            setChanged();
            notifyObservers();
        }
    
        public void btn9ChangeFalse() {
    
            btn9 = false;
            setChanged();
            notifyObservers();
        }
    
        public void btn1ChangeTrue() {
            btn1 = true;
        }
    
        public void btn2ChangeTrue() {
            btn2 = true;
        }
    
        public void btn3ChangeTrue() {
            btn3 = true;
        }
    
        public void btn4ChangeTrue() {
            btn4 = true;
        }
    
        public void btn5ChangeTrue() {
            btn5 = true;
        }
    
        public void btn6ChangeTrue() {
            btn6 = true;
        }
    
        public void btn7ChangeTrue() {
            btn7 = true;
        }
    
        public void btn8ChangeTrue() {
            btn8 = true;
        }
    
        public void btn9ChangeTrue() {
            btn9 = true;
        }
    
        public boolean btn1State() {
            return btn1;
    
        }
    
        public boolean btn2State() {
            return btn2;
    
        }
    
        public boolean btn3State() {
            return btn3;
    
        }
    
        public boolean btn4State() {
            return btn4;
    
        }
    
        public boolean btn5State() {
            return btn5;
    
        }
    
        public boolean btn6State() {
            return btn6;
    
        }
    
        public boolean btn7State() {
            return btn7;
    
        }
    
        public boolean btn8State() {
            return btn8;
    
        }
    
        public boolean btn9State() {
            return btn9;
    
        }
    }
    
    class GameController implements ActionListener {
        private GameModel model;
    
        public GameController(GameModel aModel) {
            model = aModel;
        }
    
        public void actionPerformed(ActionEvent e) {
            if (e.getActionCommand().contains("btn1")) {
                model.btn1ChangeFalse();
            }
    
            else if (e.getActionCommand().contains("btn2")) {
                model.btn2ChangeFalse();
            }
    
            else if (e.getActionCommand().contains("btn3")) {
                model.btn3ChangeFalse();
            }
    
            else if (e.getActionCommand().contains("btn4")) {
                model.btn4ChangeFalse();
            } else if (e.getActionCommand().contains("btn5")) {
                model.btn5ChangeFalse();
            } else if (e.getActionCommand().contains("btn6")) {
                model.btn6ChangeFalse();
            } else if (e.getActionCommand().contains("btn7")) {
                model.btn7ChangeFalse();
            } else if (e.getActionCommand().contains("btn8")) {
                model.btn8ChangeFalse();
            } else if (e.getActionCommand().contains("btn9")) {
                model.btn9ChangeFalse();
            }
    
        }
    
    }

    I suggest:
    • You simplify your code so that you use arrays of JButtons and of booleans, and
    • You don't fiddle with the model from within the GameView's update method or updateView method (which is called from update). This breaks the MVC contract and breaks your program and is the source of your current problem.


    As an example of the benefits of arrays, I'll give you what a simplified model class could look like:

    Java Code:
    class GameModel2 extends Observable {
        public static final int MAX_BUTTONS = 9;
        private boolean[] buttonStates = new boolean[MAX_BUTTONS];
        
        public GameModel2() {
            for (int i = 0; i < buttonStates.length; i++) {
                buttonStates[i] = true;
            }
        }
    
        // needs exception handling for if index out of range
        public void setButtonState(int index, boolean state) {
            buttonStates[index] = state;
            setChanged();
            notifyObservers();
        }
    
        // needs exception handling for if index out of range
        public boolean getButtonState(int index) {
            return buttonStates[index];
        }
    }
    Last edited by Fubarable; 05-21-2011 at 04:12 AM.

Similar Threads

  1. JSF 2.0 CRUD and different database views
    By thumper_ipw in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 03-23-2011, 03:55 PM
  2. Multiple Views
    By MrDave in forum AWT / Swing
    Replies: 8
    Last Post: 04-09-2010, 09:20 PM
  3. multiple views in one folder tab in RCP app
    By natelapp in forum Eclipse
    Replies: 1
    Last Post: 08-02-2009, 11:48 AM
  4. Relation between 2 views
    By tojas in forum SWT / JFace
    Replies: 0
    Last Post: 04-19-2009, 03:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •