0

I'm modifying an original JGoodies binding example found on JGoodies Binding: Presentation Model Property Change Example

I'm wondering however why I need to manually fire firePropertyChange(FIRST_NAME_PROPERTY, oldValue, this.firstName); to make the GUI update with the model when instead this should be automatically dealt by JGoodies.

The example below should update the JTextField by changing the model without me manually firing the fireUpdateChange from each setter! How can I accomplish this?

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);
    }
}
saimiris_devel
  • 667
  • 1
  • 6
  • 19

0 Answers0