我正在NetBeans做一个项目,是关于一家小餐馆的。
我有2个Jtables,我绑定到我的数据库,有2个形式,我用来插入数据。第一个表用于插入员工数据,第二个表用于插入项目详细信息。
我希望每次插入时刷新表中的记录。因此,我创建了2个刷新方法,传递(List l)作为参数,并从生成的代码中复制和粘贴绑定代码,并将l传递给绑定。
但是它不起作用。为什么呢?
这是我的刷新表
private void refreshItm(List l){
jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, itemsList, jTable2);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${itemid}"));
columnBinding.setColumnName("Itemid");
columnBinding.setColumnClass(Integer.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${itemname}"));
columnBinding.setColumnName("Itemname");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${availablequantity}"));
columnBinding.setColumnName("Availablequantity");
columnBinding.setColumnClass(Short.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${price}"));
columnBinding.setColumnName("Price");
columnBinding.setColumnClass(Integer.class);
bindingGroup.addBinding(jTableBinding);
jTableBinding.bind();
}
private void refreshEmp(List l){
org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, l, jTable1);
org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${empid}"));
columnBinding.setColumnName("Empid");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${empname}"));
columnBinding.setColumnName("Empname");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${address}"));
columnBinding.setColumnName("Address");
columnBinding.setColumnClass(String.class);
bindingGroup.addBinding(jTableBinding);
jTableBinding.bind();
}
您忘记更新您的结果列表(即:在你的第一个方法"itemsList"中)。寻找"initComponents"方法。在这个方法中,应该有一行来获取"itemsList",比如:
itemsList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : itemsQuery.getResultList();
将该行复制到方法的第一行
正如user1545636建议的那样,您错过了重新加载itemsList。现在我想与您分享一个更好的代码设计,以在JPA插入/更新/删除操作发生后刷新jTable:
在Netbeans中,跳转到设计选项卡,右键单击jtable并单击"自定义代码…"
从Component下拉列表中选择itemsList并在下面的下拉列表中选择"custom creation"
现在从
中更改itemsList初始化itemsList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : itemsQuery.getResultList();
itemsList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(itemsQuery.getResultList());
通过这种方式,你创建了一个可观察对象itemsList。现在在你的refreshItm方法中只写这段代码:
itemsList.clear();
itemsList.addAll( itemsQuery.getResultList());
在JPA插入/更新/删除之后调用该方法,jTable会自动刷新!
我希望你喜欢这个解决方案