JTextField中的Bean绑定不起作用

时间:2016-06-01 19:21:35

标签: java

我尝试使用Java中的bean绑定。我在一些网站上关注了代码。并试图让它工作。

在这里,我所做的是,试图在运行时用用户在textfield中输入的值更新Person的名字。但是当我点击按钮时,它包含“omg”。如果我用“”替换它,它包含该值。正如我所说,它看起来只工作一次。它出什么问题了?我在这里想念的是什么?任何人都可以帮忙解决这个问题吗?

我互换了源和目标并尝试了

import java.beans.PropertyChangeListener;
    import java.beans.PropertyChangeSupport;

    public abstract class AbstractModelObject {
        private final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

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

        public void addPropertyChangeListener(String propertyName,
                PropertyChangeListener listener) {
            propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
        }

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

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

        protected void firePropertyChange(String propertyName, Object oldValue,
                Object newValue) {
            propertyChangeSupport.firePropertyChange(propertyName, oldValue,
                    newValue);
        }
    }

    import java.beans.PropertyChangeListener;
        import java.beans.PropertyChangeSupport;

        public class PersonImpl extends AbstractModelObject implements Person{
            private String firstName; 
            private String lastName; 

            public PersonImpl()
            {

            }

            public PersonImpl(String firstName)
            {
                this.firstName = firstName;
            }

            public String getFirstName()
            { 
                System.out.println("getFirstName");
                return firstName;
            }

            public void setFirstName(String firstName) 
            { 
                System.out.println("setFirstName");
                String old = this.firstName;
                this.firstName = firstName; 
                firePropertyChange("firstName",old,firstName);
            } 
        }


        public interface Person {

            void setFirstName(String string);

            String getFirstName();

        }

            public class MyFrame extends JFrame {
                JTextField field = new JTextField("omg");
                JButton button = new JButton("Save");
                Person p = new PersonImpl();

                MyFrame()
                {

                    button.addActionListener(new ActionListener(){
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            System.out.println("first name : " + p.getFirstName());
                        }
                    });
                }
                private void initDataBinding() {
                    if(Beans.isDesignTime()){return;}
                    BeanProperty<Person, String> fNameProperty = 
                            BeanProperty.create("firstName"); 

                    BeanProperty<JTextField, String> textProperty =
                            BeanProperty.create("text"); 
                    BindingGroup bindingGroup = new BindingGroup();



AutoBinding<JTextField, String, Person, String> firstNameBinding
                    = Bindings.createAutoBinding(UpdateStrategy.READ, field, textProperty,p, fNameProperty);
                    firstNameBinding.bind();
                }
            }

1 个答案:

答案 0 :(得分:0)

通常,这种绑定将使用READWRITE创建。这似乎不是您问题的根源,但需要考虑改变。

这里要检查的第一件事是在绑定创建调用上放置一个断点,并确保它实际上被调用。

如果是,请考虑在调用.bind()之前添加绑定侦听器,并查看它是否报告任何失败的绑定操作。这应该可以帮助您快速追踪问题。