1

I am currently developing my own minesweeper. Swing follows Model-View-Controller design pattern. In MVC, I learnt whenever there is a change in model, the controller will trigger that change in view too. But In this example, I cannot trace how to make the changes in setTitle and setInfo to get reflected in view.

Here, when I set the title of the Dialog box, the actual content(model) is getting changed, But there is no corresponding change in the output(view).

//InfoDisplayer is inner class of class MenuActionListener
class InfoDisplayer extends JDialog { 
    JLabel info;
    BorderLayout infoBorderLayout = new BorderLayout();

    public InfoDisplayer(JFrame ownerFrame) {
        super(ownerFrame,true); 
        info = new JLabel();
        setFocusable(false);                        
        setSize(300,400);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLayout(infoBorderLayout);
        add(info,BorderLayout.SOUTH);
        setVisible(true);
    }

    void setInfo(JLabel info) {
        this.info = info;
    }

    public void setTitle(String title) {
        super.setTitle(title);
    }                                   
}

if ((event.getActionCommand()).equals("HowToPlay")) {
    InfoDisplayer instructionsDisplay = new InfoDisplayer(gUIManagerFrame); 
    //gUIManagerFrame is an object of its outer class,MenuActionListener
    instructionsDisplay.setTitle("INSTRUCTIONS");
    instructionsDisplay.setInfo(new JLabel("<html><h1><B>INSTRUCTIONS</B></h1></html>"));
} else {// if about is clicked!!
    InfoDisplayer aboutDisplay = new InfoDisplayer(gUIManagerFrame);
    aboutDisplay.setTitle("MineSweeper v0.1");
    aboutDisplay.setInfo(new JLabel("<html><h1><B>MineSweeperv1.0</B></h1> </html>"));
}           
Muthu Ganapathy Nathan
  • 3,199
  • 16
  • 47
  • 77

3 Answers3

3

Whenever there is a change in model, the controller will trigger that change in view.

In the Model–View–Controller pattern, when the controller updates the model, the model will notify the view, typically using the observer pattern, and the view then updates itself. The view may interrogate the model and process any resulting update. There's a more detailed answer and example here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

You will need to remove the old jlabel and add the new one to the frame.

Though it would make more sense probably to set the text on the existing label rather than a whole new label.

Tom
  • 43,583
  • 4
  • 41
  • 61
  • Neither of 2 approches worked.How will you remove the old JLabel and add new one? 1st : "void setInfo(JLabel info){ remove(this.info); this.info.setText(info.toString()); add(this.info,BorderLayout.SOUTH); }" 2nd : " void setInfo(JLabel info){ remove(this.info); this.info = info; add(info,BorderLayout.SOUTH); }" – Muthu Ganapathy Nathan Dec 06 '11 at 05:37
  • +1 for update over replace; more on MVC [here](http://stackoverflow.com/a/8401255/230513). – trashgod Dec 06 '11 at 14:13
1

Swing indeed has a model and a view side. For example in a JTable the JTable is the view and the TableModel is the model. When you construct a JTable, you need to pass it a model either during construction or by using the setter. The JTable will then add a listener to model to get informed about any model changes. You can see this listener as the controller.

However, this does not mean that when you use an arbitrary combination of Swing classes they will auto-magically get informed about each other changes. In your case, the label is certainly not 'the model' of your dialog, and there is no such thing as a 'controller' between your label and the dialog. When you make such a change, you need to inform the dialog yourself (and probably add the label to your dialog as well).

Oh, and I would recommend changing your setTitle method into

public void setTitle( String aTitle ){
    super.setTitle( aTittle );
}

or remove it completely. This will avoid a StackOverflowException

Robin
  • 36,233
  • 5
  • 47
  • 99