列表内对象的PropertyChangeListener



我的问题是,当我更改模型属性的值时,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);
    }
}

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

监听器wird zustackOverflow.allInOne.MyPropertyChangeListener@cee47f1hinzugefügtmit属性btnTextjava.beans.PropertyChangeEvent[propertyName=btnText;oldValue=null;newValue=null;propagationId=null;来源=stackOverflow.allInOne.Model2@6fb525d8]收到火灾

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

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

因此,正如我在最初的问题中所提到的,在我看来,

x.addPropertyChangeListener(Model2.BTN_TXT, myListener);

不总是有效的。

错误非常简单。火和听众合作了。唯一的问题是propertychangesupport从未真正启动,因为我每次都传递相同的值:

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

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

因此,正确的例子是:

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

感谢的帮助

相关内容

  • 没有找到相关文章

最新更新