开发者

Observer/Observable Help!

开发者 https://www.devze.com 2023-02-05 04:58 出处:网络
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.

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.


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();
        }
    });
}
0

精彩评论

暂无评论...
验证码 换一张
取 消