0

我创建了一个 jpanel,其中包含使用 netbeans 右键单击​​ -> 绑定 -> 元素和刷新按钮绑定到数据库表的 jtable。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();
    } 
4

1 回答 1

0

尝试

g.unbind();
g.setSourceObject(formationList);
g.bind();
于 2016-03-23T19:05:45.203 回答