Jgoodies绑定不触发firePropertyChange

时间:2015-06-05 13:12:05

标签: java swing jgoodies

我正在修改JGoodies Binding: Presentation Model Property Change Example

上找到的原始JGoodies绑定示例

我想知道为什么我需要手动触发firePropertyChange(FIRST_NAME_PROPERTY,oldValue,this.firstName);相反,这应该由JGoodies自动处理。

下面的示例应该通过更改模型来更新JTextField,而无需我从每个setter手动触发fireUpdateChange! 我怎么能做到这一点?

import java.awt.event.ActionEvent;
import javax.swing.*;
import com.jgoodies.binding.*;
import com.jgoodies.binding.adapter.Bindings;
import com.jgoodies.binding.beans.BeanAdapter;
import java.util.Random;

public class PresentationModelPropertyChangeExample extends JPanel
{

    private PersonModel personModel;
    BeanAdapter beanAdapter;

    public PresentationModelPropertyChangeExample()
    {
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        this.personModel = new PersonModel();
        beanAdapter = new BeanAdapter(personModel, true);
        JTextField firstNameTextField = new JTextField();

        Bindings.bind(firstNameTextField, beanAdapter.getValueModel(personModel.FIRST_NAME_PROPERTY));

        add(firstNameTextField);
        add(new JButton(new ChangeBeanAction()));
    }

    private class ChangeBeanAction extends AbstractAction
    {

        public ChangeBeanAction()
        {
            super("Change PersonBean");
        }

        @Override
        public void actionPerformed(ActionEvent event)
        {
            Random rand = new Random(1000);
            personModel.setFirstName("" + rand.nextInt());
        }
    }

    public class PersonModel extends PresentationModel
    {

        private String firstName;
        public static final String FIRST_NAME_PROPERTY = "firstName";

        public String getFirstName()
        {
            return firstName;
        }

        public void setFirstName(String firstName)
        {
            String oldValue = this.firstName;
            this.firstName = firstName;
            //firePropertyChange(FIRST_NAME_PROPERTY, oldValue, this.firstName);
        }
    }

    public static void main(String[] a)
    {
        JFrame f = new JFrame("Presentation PropertyChange Example");
        f.setDefaultCloseOperation(2);
        f.add(new PresentationModelPropertyChangeExample());
        f.pack();
        f.setVisible(true);
    }
}

0 个答案:

没有答案