我创建了一个jpanel,其中包含一个绑定到数据库表的jtable,使用netbeans右键单击 - > bind - >元素和刷新按钮。 jpanel位于jframe内的jtabbedpane内。 我想在添加或删除元素后刷新jtable,并且厌倦了我在谷歌上找到的很多解决方案,但对我来说没有任何作用。
jpanel类:
public class Formations extends javax.swing.JPanel
public Formations() {
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
FormationPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("PUfile").createEntityManager();
formationQuery = java.beans.Beans.isDesignTime() ? null : FormationPUEntityManager.createQuery("SELECT f FROM Formation f");
formationList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : formationQuery.getResultList();
btnRefresh = new javax.swing.ColoredButton();
tblContenuFormations = new javax.swing.JTable(){
public boolean isCellEditable(int row, int column) {
return false;
}
};
org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, formationList, tblContenuFormations);
org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${theme}"));
columnBinding.setColumnName("Col1");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${Col1}"));
columnBinding.setColumnName("Col2");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${Col2}"));
columnBinding.setColumnName("Col3");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${Col3}"));
columnBinding.setColumnName("Col4");
columnBinding.setColumnClass(java.util.Collection.class);
bindingGroup.addBinding(jTableBinding);
jTableBinding.bind();
tblContenuFormations.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
tblContenuFormationsPropertyChange(evt);
}
});
jScrollPane1.setViewportView(tblContenuFormations);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(....));
bindingGroup.bind();
}
private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {
//here goes the refresh code
}
// Variables declaration - do not modify
private javax.persistence.EntityManager FormationPUEntityManager;
private javax.swing.JButton btnRefresh;
private java.util.List<dz.pcmax.entities.Formation> formationList;
private javax.persistence.Query formationQuery;
private javax.swing.JScrollPane jScrollPane1;
private org.jdesktop.beansbinding.BindingGroup bindingGroup;
// End of variables declaration
}
我试过的解决方案:
1
private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {
Binding g = bindingGroup.getBindings().get(0);
g.unbind();
g.bind();
}
2
private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {
Binding g = bindingGroup.getBindings().get(0);
g.unbind();
g.bind();
this.validate();
this.repaint();
}
3
private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {
org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, formationList, tblContenuFormations);
org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${theme}"));
columnBinding.setColumnName("Theme");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${description}"));
columnBinding.setColumnName("Description");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${objectifs}"));
columnBinding.setColumnName("Objectifs");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${niveauCollection}"));
columnBinding.setColumnName("Niveau Collection");
columnBinding.setColumnClass(java.util.Collection.class);
bindingGroup.addBinding(jTableBinding);
jTableBinding.bind();
getTopLevelAncestor().revalidate();
getTopLevelAncestor().repaint();
}
答案 0 :(得分:0)
试
g.unbind();
g.setSourceObject(formationList);
g.bind();