1
我使用netbeans右鍵單擊 - >綁定 - >元素和刷新按鈕創建了一個包含綁定到數據庫表的jtable的jpanel。 jpanel位於jframe內部的jtabbedpane內。 我想在添加或刪除元素後刷新jtable,並厭倦了我在谷歌上找到的很多解決方案,但沒有任何適用於我的解決方案。使用org.jdesktop.swingbinding.JTableBinding刷新綁定的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
}
溶液我嘗試:
private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {
Binding g = bindingGroup.getBindings().get(0);
g.unbind();
g.bind();
}
private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {
Binding g = bindingGroup.getBindings().get(0);
g.unbind();
g.bind();
this.validate();
this.repaint();
}
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();
}