2015-03-02 59 views
0

我正在Java7 SE上用Eclipse Juno,WindowBuilder「Swing Automatic Databinding」(beansbinding-1.2.1.jar)開發我的第一個JDialog。 我很好奇測試自動數據綁定WindowBuilder上的雙向數據綁定似乎不起作用

我得到了一個編輯對話框的類用戶藉助Eclipse數據綁定GUI只。

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實例的正確初始值。當我修改文本字段文本,然後單擊確定按鈕時,我看不到用戶實例的更改。 這裏是我的對話框代碼.....

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,所以我不明白爲什麼它不更新用戶實例! 有什麼想法?

感謝, 馬爾科

回答

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); 
    } 
}