0

Greetings,

I have been studying Obserser/Observable implementation to Model-View-Controller. I may also misused the pattern but here's what I've done so far. In my code. When Submit was press, it triggers the makeChange() of the Model. But never triggers the update() of Testing.

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Testing implements Observer {

    public Testing() {
        model.addObserver(this);
        loadListener1();        
    }

    private void loadListener1() {
        view1.submitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                model = new Model(view1.data1Field.getText(), view1.data2Field.getText());
                model.makeChange();
                model.notifyObservers();
            }
        });
    }  

    public void update(Observable o, Object arg) { System.out.println("Notify - Observer"); }

    public static void main(String[] a) { Testing testing = new Testing(); }

    private View view1 = new View("1");    
    private Model model = new Model();

}

class View extends JFrame  {

    public View(String frame) {
        super("Frame: " + frame);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300, 300);
        setVisible(true);
        setLayout(new GridLayout(3, 2));
        add(data1Label); add(data1Field);
        add(data2Label); add(data2Field);
        add(submitButton); add(cancelButton);
    }

    private final JLabel data1Label = new JLabel("Data1");
    private final JLabel data2Label = new JLabel("Data2");
        public final JTextField data1Field = new JTextField();
    public final JTextField data2Field = new JTextField();
    public final JButton submitButton = new JButton("Submit");
    public final JButton cancelButton = new JButton("Cancel");

}

class Model extends Observable {

    public Model() { }
    public Model(String data1, String data2) {
        setData1(data1);
        setData2(data2);
    }
    public String getData1() { return data1; }
    public final void setData1(String data1) { this.data1 = data1; }
    public String getData2() { return data2; }
    public final void setData2(String data2) { this.data2 = data2; }
    public void makeChange() {
        setChanged();
        notifyObservers();
        System.out.println("Notify - Observable");
    }
    private String data1;
    private String data2;

}

Can you guide me thru? Your reply is highly appreciated.

Thanks, Cyril H.

Mihai Toader
  • 12,041
  • 1
  • 29
  • 33
Cyril Horad
  • 1,555
  • 3
  • 23
  • 35
  • When you step through you code with a debugger, what do you see? – Peter Lawrey Jan 14 '11 at 16:25
  • @Peter: I think the update() method was never triggered. But don't know why... – Cyril Horad Jan 14 '11 at 16:29
  • @Cyril, which is why I suggested you use a debugger. Place a breakpoint where you know the code is running and step through it until it doesn't do what you expect. – Peter Lawrey Jan 14 '11 at 16:39
  • `java.util.Observable`/`Observer` are best left alone. There's no value in them. – Tom Hawtin - tackline Jan 14 '11 at 17:31
  • How many calls to new Model() does your code have? In other words, how many Model objects are present in your program? Which one is being observed? Which one is notifying the observers? What effect will this have on the output? – Hovercraft Full Of Eels Jan 14 '11 at 16:32
  • Currently, there only one Model and its only called once. But later I have to pass the model to different controller(I'll update the code above for this). But the code for now must have these outputs(can't tell what goes first) - "Notify - Observable" and "Notify - Observer". The testing notifies the model. – Cyril Horad Jan 14 '11 at 16:37
  • No there are two model objects and if you search the code you'll see that new Model is called twice. I was trying to nudge you to figure this out on your own, but then lweller then posted what I was trying to get you to do. – Hovercraft Full Of Eels Jan 14 '11 at 16:45

1 Answers1

0

In constructor of clas Testing your register itself to instance of Model that is created during field initialisation.

In actionPerformed method of submit button you creatd a new instance of Model and later call on this new instance the method notifyObservers which has no registered observers!

Try this code:

private void loadListener1() {
    view1.submitButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            model.setData1(view1.data1Field.getText()),
            model.setData2(view1.data2Field.getText());
            model.makeChange();
        }
    });
}
lweller
  • 11,077
  • 3
  • 34
  • 38