数据绑定Swing Eclipse beansbinding-1.2.1.jar WindowBuilderPro

时间:2018-10-29 10:57:17

标签: java eclipse swing data-binding windowbuilder

我正在尝试使用WindowBuilder在Eclipse IDE下使用Databinding创建新的GUI。 首先,我将beanbinding-1.2.1.jar添加到项目中,并获得WindowBuilder的“绑定”选项卡。

我创建了一个Bean(JavaBean),一个Main Class(运行)和两个不同的JFrame(LabelWindow和TextFieldWindow)。所有内容均在帖子下方。 在LabelWindow中,我使用了JLabel来显示Bean中的数据(名称:“ Foo”)。多数民众赞成在完美。如果我更改了Bean中的数据,那么JLabel中的数据也会更改,如果我更改了JLabel中的数据,那么Bean也将更改。

在TextFieldWindow中,我使用了JTextField来显示Bean中的数据。那并不像JLabel那样工作。 Gui显示了来自Bean“ Foo”的数据,如果我更改Beans值,它也会在JTextField中消失。就是这样。 但是,如果我更改JTextField的值,则Beans值不会改变。

整个JFrame是由WindowBuilder插件中的“ Swing自动数据绑定向导”生成的,并且与JLabel一起使用,与JTextField一起仅在一个方向上工作。

有人可以告诉我哪里出了什么问题以及如何解决此问题(我更喜欢使用WindowBuilder / Bindingtab的方法,我只是不想手动更改太多内容。)

这是我的代码:

JavaBean.java

package def;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

public class JavaBean {
    private String name = "Foo";
    private PropertyChangeSupport pcs = new PropertyChangeSupport(this);

    public String getName() {
        return name;
    }

    public void setName(String name) {
        String oldValue = this.name;
        this.name = name;
        pcs.firePropertyChange("name", oldValue, name);
    }

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

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

Run.java

package def;

import java.awt.EventQueue;

public class Run {

    public static void main(String[] args) {
        //Switch these two Versions to Test either the Label Version or the TextField Version
        //EventQueue.invokeLater( () -> new LabelWindow().setVisible(true));
        EventQueue.invokeLater( () -> new TextFieldWindow().setVisible(true));
    }

}

LabelWindow.java

package def;

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

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.Bindings;

public class LabelWindow extends JFrame {

    private static final long serialVersionUID = 1L;
    private BindingGroup m_bindingGroup;
    private JPanel m_contentPane;
    private JavaBean javaBean = new JavaBean();
    private JLabel nameJLabel;

    public LabelWindow() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        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 };
        gridBagLayout.columnWeights = new double[] { 0.0, 1.0, 1.0E-4 };
        gridBagLayout.rowWeights = new double[] { 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);

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

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

    protected BindingGroup initDataBindings() {
        BeanProperty<JavaBean, String> nameProperty = BeanProperty.create("name");
        BeanProperty<JLabel, String> textProperty = BeanProperty.create("text");
        AutoBinding<JavaBean, String, JLabel, String> autoBinding = Bindings
                .createAutoBinding(AutoBinding.UpdateStrategy.READ_WRITE, javaBean, nameProperty, nameJLabel,
                        textProperty);
        autoBinding.bind();
        //
        BindingGroup bindingGroup = new BindingGroup();
        bindingGroup.addBinding(autoBinding);
        //
        return bindingGroup;
    }
}

TextFieldWindow.java

package def;

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

import javax.swing.JFrame;
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;

public class TextFieldWindow extends JFrame {

    private static final long serialVersionUID = 1L;
    private BindingGroup m_bindingGroup;
    private JPanel m_contentPane;
    private JavaBean javaBean = new JavaBean();
    private JTextField nameJTextField;

    public TextFieldWindow() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        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 };
        gridBagLayout.columnWeights = new double[] { 0.0, 1.0, 1.0E-4 };
        gridBagLayout.rowWeights = new double[] { 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, 0, 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, 0, 0);
        componentGbc_0.fill = GridBagConstraints.HORIZONTAL;
        componentGbc_0.gridx = 1;
        componentGbc_0.gridy = 0;
        m_contentPane.add(nameJTextField, componentGbc_0);

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

    protected BindingGroup initDataBindings() {
        BeanProperty<JavaBean, String> nameProperty = BeanProperty.create("name");
        BeanProperty<JTextField, String> textProperty = BeanProperty.create("text");
        AutoBinding<JavaBean, String, JTextField, String> autoBinding = Bindings
                .createAutoBinding(AutoBinding.UpdateStrategy.READ_WRITE, javaBean, nameProperty, nameJTextField,
                        textProperty);
        autoBinding.bind();
        //
        BindingGroup bindingGroup = new BindingGroup();
        bindingGroup.addBinding(autoBinding);
        //
        return bindingGroup;
    }

    public JavaBean getJavaBean() {
        return javaBean;
    }

    public void setJavaBean(JavaBean newJavaBean) {
        setJavaBean(newJavaBean, true);
    }

    public void setJavaBean(JavaBean newJavaBean, boolean update) {
        javaBean = newJavaBean;
        if (update) {
            if (m_bindingGroup != null) {
                m_bindingGroup.unbind();
                m_bindingGroup = null;
            }
            if (javaBean != null) {
                m_bindingGroup = initDataBindings();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在您的课程Run中,您将创建两个顶级容器。我尚未运行您发布的代码,但从未尝试在一个应用程序中创建两个顶级容器。我建议创建一个同时包含JFrameJLabel的{​​{1}}。你为什么没有理由?