0

我的问题是,当我从模型的属性中更改值时,PropertyChangeSupport会触发但PropertyChangeListener永远不会到达视图中的值。所以我想我通过添加监听器做错了。

任何类型的输入表示赞赏。

package stackOverflow.allInOne;

import java.awt.FlowLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import static java.lang.System.getProperty;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;


public class AllInOne {
        public static void main(String[] args) {
        final Model Model = new Model();
        final Controller Controller = new Controller(Model);
        Controller.initializeView();
    }
}

class View extends JFrame{

        private final Model model;
    private final Controller controller;

        JTextField fldAny;
    JButton btnAny; 

    public View(Model model, Controller controller) {
        super("SBB app");
        this.model = model;
        this.controller = controller;
    }

    public void createAndShow() {
        initializeComponents();
        JPanel contents = layoutComponents();
        addActionEvents();
        addPropertyChangeListeners();  

        add(contents);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
        }

    private void initializeComponents() {
        fldAny = new JTextField("----------");
        btnAny = new JButton("klick me");
    }

    private JPanel layoutComponents() {
        JPanel anyPanel = new JPanel(new FlowLayout());
        anyPanel.add(fldAny);
        anyPanel.add(btnAny);
        return anyPanel;
    }

    private void addActionEvents() {
        fldAny.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void changedUpdate(DocumentEvent e) {
                change();
            }
            @Override
            public void insertUpdate(DocumentEvent e) {
                change();
            }
            @Override
            public void removeUpdate(DocumentEvent e) {
                change();
            }
            public void change() {
                                 System.out.println("action event fired");
                                 controller.setNewValue(fldAny.getText());
             }
        });
    }

    private void addPropertyChangeListeners() {
        PropertyChangeListener myListener = new MyPropertyChangeListener(this);
                List<Model2> xx = model.getAllModel2();
                for(Model2 x : xx){
                    x.addPropertyChangeListener(Model2.BTN_TXT, myListener);
                }
    }
}

class MyPropertyChangeListener implements PropertyChangeListener{
    private View view;

    public MyPropertyChangeListener(View view) {
        this.view = view;
    }

         @Override
         public void propertyChange(PropertyChangeEvent evt) {
                 System.out.println(evt);
                 System.out.println("Receved the fire");
                 view.btnAny.setText(view.btnAny.getText() + "1");
         }
}

 class Model2{
    private String fldText;
    private String btnText;

    public static final String BTN_TXT = "btnText";
    private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);

    Model2(String fldText, String btnText) {
        this.fldText = fldText;
        this.btnText = btnText;
    } 

    public void setFldText(String fldText) {
        this.fldText = fldText;
    }

    public String getFldText() {
        return fldText;
    }

    public String getBtnText() {
        return btnText;
    }

    public void setBtnText(String btnText) {
        System.err.println("Model fires");
        pcs.firePropertyChange(BTN_TXT, btnText, this.btnText = btnText);
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        pcs.addPropertyChangeListener(listener);
    }

    public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
        System.out.println("Listener wird zu " +listener.toString() + " hinzugefügt mit property " +  propertyName);
                pcs.addPropertyChangeListener(propertyName, listener);
        final Object value = getProperty(propertyName);
        listener.propertyChange(new PropertyChangeEvent(this, propertyName,
                value, value));
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        pcs.removePropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
        pcs.removePropertyChangeListener(propertyName, listener);
    }

    public PropertyChangeListener[] getPropertyChangeListeners() {
        return pcs.getPropertyChangeListeners();
    }

    public PropertyChangeListener[] getPropertyChangeListeners(String propertyName) {
        return pcs.getPropertyChangeListeners(propertyName);
    }

}

 class Model {
    private List<Model2> model2List = new ArrayList<Model2>();


    public Model() {
        model2List.add(new Model2("fldText","btnText"));
    }

    public Model2 getSelectdModel2(){
        return model2List.get(0);
    }

    List<Model2> getAllModel2() {
        return model2List;
    }
}

 class Controller {
        private final Model model;

        public Controller(Model model) {
        this.model = model;

    }

    public void initializeView() {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                View view = new View(model, Controller.this);
                view.createAndShow();
            }
        });
    }

    void setNewValue(String text) {
        System.out.println("Controller sets new value");
        model.getSelectdModel2().setBtnText(text);
    }

}

当我启动应用程序时,我目前得到以下输出:

监听器 zu stackOverflow.allInOne.MyPropertyChangeListener@cee47f1 hinzugefügt mit property btnText java.beans.PropertyChangeEvent[propertyName=btnText; 旧值=空;新值=空;传播标识=空;source=stackOverflow.allInOne.Model2@6fb525d8] 收到火

所以我有点困惑为什么我会着火,因为我在添加监听器之前设置了 btnTxt 的值。无论如何,如果我更改字段中的文本,那么我将得到以下输出:

动作事件触发控制器设置新值模型触发

所以正如我在最初的问题中提到的那样,在我看来

x.addPropertyChangeListener(Model2.BTN_TXT, myListener);

并不总是有效。

4

1 回答 1

0

错误是如此简单。火和听众工作。唯一的问题是 propertychangesupport 从未真正触发过,因为我每次都传递相同的值:

pcs.firePropertyChange(BTN_TXT, btnText, this.btnText = btnText);

而第二个参数应该是旧值,第三个是新值。因为我已经为我的旧变量分配了新值this.btnText = btnText,所以 pcs 认为这些值相同并且没有改变任何东西。

所以正确的例子是:

pcs.firePropertyChange(BTN_TXT, this.btnText, btnText); this.btnText = btnText;

谢谢您的帮助

于 2013-12-16T18:03:36.167 回答