我有一个简单的 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() + "\n");
}
JOptionPane.showMessageDialog(null, "The elements are:\n\n" + builder.toString());
}
}