我正在修改在JGoodies Binding:Presentation Model Property Change Example上找到的原始 JGoodies 绑定示例
但是我想知道为什么我需要手动触发 firePropertyChange(FIRST_NAME_PROPERTY, oldValue, this.firstName); 使用模型进行 GUI 更新,而这应该由 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);
}
}