BindingBeans中的复合值未更新

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BindingBeans中的复合值未更新相关的知识,希望对你有一定的参考价值。

我有一个简单的POJO如下:

class A{
    private int a;
    private int b;

    public int getC(){
        return a + b;
    }

    //getters and setters for a and b
}

该实体使用BindingBeans绑定到JTable。对a和b所做的任何更改都会在JTable中正确处理并显示,但是c不会更新。

我想如果更新a或b,我需要触发属性更改事件,但实际上没有任何想法。有任何想法吗?

编辑:

我创建了一个简单的例子:

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.Serializable;


public class POJO implements Serializable{

    private static final Gson gson = new GsonBuilder().create();

    private Integer a;
    private Integer b;

    public Integer getC() {
        return a + b;
    }

    public Integer getA() {
        return a;
    }

    public void setA(Integer a) {
        this.a = a;
    }

    public Integer getB() {
        return b;
    }

    public void setB(Integer b) {
        this.b = b;
    }

    @Override
    public String toString(){
        return gson.toJson(this);
    }
}

和主类

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.swingbinding.JTableBinding;
import org.jdesktop.swingbinding.SwingBindings;


public class Example implements ActionListener{

    private JTable table;
    private JFrame frame;
    private JTableBinding tableBinding;
    private List<POJO> elements;

    public static void main(String [] args){
        new Example();
    }

    public Example(){
        initData();
        initGui();
        initBindings();
        frame.pack();
    }

    private void initData(){
        elements = new ArrayList<POJO>();
        POJO pojoA = new POJO();
        pojoA.setA(1);
        pojoA.setB(2);
        POJO pojoB = new POJO();
        pojoB.setA(3);
        pojoB.setB(4);
        elements.add(pojoA);
        elements.add(pojoB);
    }

    private void initGui(){
         frame = new JFrame();
         table = new JTable();
         frame.getContentPane().setLayout(new BorderLayout());
         JPanel tablePanel = new JPanel();
         tablePanel.setLayout(new GridLayout());
         tablePanel.add(table);
         frame.getContentPane().add(BorderLayout.CENTER,tablePanel);
         JButton button = new JButton("Show Current Elements");
         button.addActionListener(this);
         frame.getContentPane().add(BorderLayout.SOUTH,button);
         frame.setLocationRelativeTo(null);
         frame.setVisible(true);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void initBindings(){
        tableBinding = SwingBindings.createJTableBinding(
                AutoBinding.UpdateStrategy.READ_WRITE, elements,
                table);
        addBinding("a", "A", Integer.class, true);
        addBinding("b", "B", Integer.class, true);
        addBinding("c", "C", Integer.class, false);
        tableBinding.bind();
    }

    private void addBinding(String properyName, String columnName, Class clazz, boolean editable) {
        BeanProperty beanProperty = BeanProperty.create(properyName);
        JTableBinding.ColumnBinding columnBinding = tableBinding.addColumnBinding(beanProperty);
        columnBinding.setColumnName(columnName);
        columnBinding.setColumnClass(clazz);
        columnBinding.setEditable(editable);
    }

    public void actionPerformed(ActionEvent ae) {
        StringBuilder builder = new StringBuilder();
        for(POJO pojo : elements){
            builder.append(pojo.toString() + "
");
        }
        JOptionPane.showMessageDialog(null, "The elements are:

" + builder.toString());
    }
}
答案

为了拥有正确的bean,您需要触发事件

private final PropertyChangeSupport = new PropertyChangeSupport( this );

public void setA(Integer a) {
  Integer old = this.a;
  this.a = a;
  propertyChangeSupport.firePropertyChange( "A", old, this.a );
}

C是派生属性。如果你只有一个吸气剂而没有二传手,那么不确定它是否会起作用。但是,如果C发生变化,您很可能需要触发事件。因为当A或B改变时它会改变,我会将他们的setter更新为

public void setA(Integer a) {
  Integer oldA = this.a;
  Integer oldC = getC();
  this.a = a;
  propertyChangeSupport.firePropertyChange( "A", oldA, this.a );
  propertyChangeSupport.firePropertyChange( "C", oldC, getC() );
}

当然,这同样适用于B属性。

但正如之前评论的那样,我没有使用该绑定框架的经验。所以不确定这是否会解决您的问题,我还没有测试过(不打算先下载/安装该框架)。

以上是关于BindingBeans中的复合值未更新的主要内容,如果未能解决你的问题,请参考以下文章

嵌套 Rails 表单中的动态值未更新

颤振列表值未更新

RegSetValueEx返回成功,但是注册表值未在regedit中更新

反应:useContext 值未在嵌套函数中更新

我的 SKSpriteNode 速度值未正确更新

Svelte 绑定选择值未在获取时更新