WindowBuilder上的双向数据绑定似乎不起作用

时间:2015-03-02 22:07:40

标签: java eclipse data-binding windowbuilder

我正在Java7 SE上使用Eclipse Juno,WindowBuilder,“Swing Automatic Databinding”(beansbinding-1.2.1.jar)开发我的第一个JDialog。 我很想测试自动数据绑定

我只在Eclipse Databinding GUI的帮助下获得了User类的编辑对话框。

package it.marcuzzi.databindtest;

public class User {
    private String name;
    private int age;

    public User() {}

    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }   
}

我的对话框打开,其文本字段包含来自User实例的正确初始值。当我修改文本字段文本然后单击“确定”按钮时,我看不到对User实例的任何更改。 这是我的对话框代码.....

package it.marcuzzi.databindtest;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.Bindings;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class UserJDialog extends JDialog {
    private static final long serialVersionUID = 7043021546503322090L;
    private BindingGroup m_bindingGroup;
    private JPanel m_contentPane;
    private it.marcuzzi.databindtest.User user = new    it.marcuzzi.databindtest.User();
    private JTextField nameJTextField;
    private JTextField ageJTextField;
    private JButton btnOk;

    public static void main(String[] args) {
        try {
            UserJDialog dialog = new UserJDialog();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public UserJDialog() {
        user.setName("marco");
        user.setAge(10);

        setBounds(100, 100, 450, 300);
        m_contentPane = new JPanel();
        setContentPane(m_contentPane);
        //
        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWidths = new int[] { 0, 0, 0 };
        gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0, 0, 0 };
        gridBagLayout.columnWeights = new double[] { 0.0, 1.0, 1.0E-4 };
        gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4 };
        m_contentPane.setLayout(gridBagLayout);

        JLabel nameLabel = new JLabel("Name:");
        GridBagConstraints labelGbc_0 = new GridBagConstraints();
        labelGbc_0.insets = new Insets(5, 5, 5, 5);
        labelGbc_0.gridx = 0;
        labelGbc_0.gridy = 0;
        m_contentPane.add(nameLabel, labelGbc_0);

        nameJTextField = new JTextField();
        GridBagConstraints componentGbc_0 = new GridBagConstraints();
        componentGbc_0.insets = new Insets(5, 0, 5, 0);
        componentGbc_0.fill = GridBagConstraints.HORIZONTAL;
        componentGbc_0.gridx = 1;
        componentGbc_0.gridy = 0;
        m_contentPane.add(nameJTextField, componentGbc_0);

        JLabel ageLabel = new JLabel("Age:");
        GridBagConstraints labelGbc_1 = new GridBagConstraints();
        labelGbc_1.insets = new Insets(5, 5, 5, 5);
        labelGbc_1.gridx = 0;
        labelGbc_1.gridy = 1;
        m_contentPane.add(ageLabel, labelGbc_1);

        ageJTextField = new JTextField();
        GridBagConstraints componentGbc_1 = new GridBagConstraints();
        componentGbc_1.insets = new Insets(5, 0, 5, 0);
        componentGbc_1.fill = GridBagConstraints.HORIZONTAL;
        componentGbc_1.gridx = 1;
        componentGbc_1.gridy = 1;
        m_contentPane.add(ageJTextField, componentGbc_1);

        btnOk = new JButton("Ok");
        btnOk.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println( "Name: "+user.getName()+"\tAge: "+user.getAge());
                UserJDialog.this.setVisible(false);
            }
        });
        GridBagConstraints gbc_btnOk = new GridBagConstraints();
        gbc_btnOk.gridx = 1;
        gbc_btnOk.gridy = 4;
        m_contentPane.add(btnOk, gbc_btnOk);

        if (user != null) {
            m_bindingGroup = initDataBindings();
        }
    }

    protected BindingGroup initDataBindings() {
        BeanProperty<it.marcuzzi.databindtest.User, java.lang.String> nameProperty = BeanProperty.create("name");
        BeanProperty<javax.swing.JTextField, java.lang.String> textProperty = BeanProperty.create("text");
        AutoBinding<it.marcuzzi.databindtest.User, java.lang.String, javax.swing.JTextField, java.lang.String> autoBinding = Bindings
                    .createAutoBinding(AutoBinding.UpdateStrategy.READ_WRITE, user, nameProperty, nameJTextField, textProperty);
        autoBinding.bind();
        //
        BeanProperty<it.marcuzzi.databindtest.User, java.lang.Integer> ageProperty = BeanProperty.create("age");
        BeanProperty<javax.swing.JTextField, java.lang.String> textProperty_1 = BeanProperty.create("text");
        AutoBinding<it.marcuzzi.databindtest.User, java.lang.Integer, javax.swing.JTextField, java.lang.String> autoBinding_1 = Bindings
                    .createAutoBinding(AutoBinding.UpdateStrategy.READ_WRITE, user, ageProperty, ageJTextField, textProperty_1);
        autoBinding_1.bind();
        //
        BindingGroup bindingGroup = new BindingGroup();
        bindingGroup.addBinding(autoBinding);
        bindingGroup.addBinding(autoBinding_1);
        //
        return bindingGroup;
    }

    public it.marcuzzi.databindtest.User getUser() {
        return user;
    }

    public void setUser(it.marcuzzi.databindtest.User newUser) {
        setUser(newUser, true);
    }

    public void setUser(it.marcuzzi.databindtest.User newUser, boolean update) {
        user = newUser;
        if (update) {
            if (m_bindingGroup != null) {
                m_bindingGroup.unbind();
                m_bindingGroup = null;
            }
            if (user != null) {
                m_bindingGroup = initDataBindings();
            }
        }
    }
}

绑定更新策略都是READ_WRITE所以我无法理解它为什么不更新User实例! 任何的想法 ?

谢谢, 马可

1 个答案:

答案 0 :(得分:0)

实际上,它正在更新用户实例,但您的小部件未获得通知。您必须在setter中触发PropertyChangeEvent。这是一个例子:

import java.beans.*;

public class Foo {

  private int bar;
  private PropertyChangeSupport pcs = new PropertyChangeSupport(this);

  public int getBar() { return bar; }

  public void setBar(int bar) {
    int oldValue = this.bar;
    this.bar =  bar;
    pcs.firePropertyChange("bar", oldValue, bar);
  }

  public void addPropertyChangeListener(PropertyChangeListener pcl) {
    pcs.addPropertyChangeListener(pcl);
  }

  public void removePropertyChangeListener(PropertyChangeListener pcl) {
    pcs.removePropertyChangeListener(pcl);
  }
}