Ciao a tutti.
Sono riuscito a visualizzare in una JTable il contenuto di una tabella del mio db (mySQL). Ora vorrei capire come è possibile mantenerla sincronizzata o quantomeno come è possibile fare tutte le modifiche non appena clicco il pulsante OK.
Vi posto il codice che ho usato:
codice:
package myPack;
public class FileTable extends javax.swing.JDialog {
/** A return status code - returned if Cancel button has been pressed */
public static final int RET_CANCEL = 0;
/** A return status code - returned if OK button has been pressed */
public static final int RET_OK = 1;
/** Creates new form FileTable */
public FileTable(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
}
/** @return the return status of this dialog - one of RET_OK or RET_CANCEL */
public int getReturnStatus() {
return returnStatus;
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
sf_dataPUEntityManager = javax.persistence.Persistence.createEntityManagerFactory("sf_dataPU").createEntityManager();
fileQuery = sf_dataPUEntityManager.createQuery("SELECT f FROM File f");
fileList = fileQuery.getResultList();
okButton = new javax.swing.JButton();
cancelButton = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
closeDialog(evt);
}
});
okButton.setText("OK");
okButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okButtonActionPerformed(evt);
}
});
cancelButton.setText("Cancel");
cancelButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelButtonActionPerformed(evt);
}
});
jTable1.setColumnSelectionAllowed(true);
jTable1.getTableHeader().setReorderingAllowed(false);
org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, fileList, jTable1);
org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${linkFile}"));
columnBinding.setColumnName("Link File");
columnBinding.setColumnClass(String.class);
columnBinding.setEditable(false);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${usato}"));
columnBinding.setColumnName("Usato");
columnBinding.setColumnClass(Boolean.class);
bindingGroup.addBinding(jTableBinding);
jTableBinding.bind();
jScrollPane1.setViewportView(jTable1);
jTable1.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
jTable1.getColumnModel().getColumn(1).setMinWidth(10);
jTable1.getColumnModel().getColumn(1).setCellEditor(null);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(250, Short.MAX_VALUE)
.addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(cancelButton)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(15, Short.MAX_VALUE))
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 97, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(cancelButton)
.addComponent(okButton))
.addContainerGap())
);
bindingGroup.bind();
pack();
}// </editor-fold>
private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {
doClose(RET_OK);
}
private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
doClose(RET_CANCEL);
}
/** Closes the dialog */
private void closeDialog(java.awt.event.WindowEvent evt) {
doClose(RET_CANCEL);
}
private void doClose(int retStatus) {
returnStatus = retStatus;
setVisible(false);
dispose();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
FileTable dialog = new FileTable(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton cancelButton;
private java.util.List<myPack.File> fileList;
private javax.persistence.Query fileQuery;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JButton okButton;
private javax.persistence.EntityManager sf_dataPUEntityManager;
private org.jdesktop.beansbinding.BindingGroup bindingGroup;
// End of variables declaration
private int returnStatus = RET_CANCEL;
}
Vi posto anche la classe File.class:
codice:
package myPack;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;
@Entity
@Table(name = "file")
@NamedQueries({@NamedQuery(name = "File.findById", query = "SELECT f FROM File f WHERE f.id = :id"), @NamedQuery(name = "File.findByLinkFile", query = "SELECT f FROM File f WHERE f.linkFile = :linkFile"), @NamedQuery(name = "File.findByNomeFile", query = "SELECT f FROM File f WHERE f.nomeFile = :nomeFile"), @NamedQuery(name = "File.findByUsato", query = "SELECT f FROM File f WHERE f.usato = :usato"), @NamedQuery(name = "File.findByPercorsoLocale", query = "SELECT f FROM File f WHERE f.percorsoLocale = :percorsoLocale"), @NamedQuery(name = "File.findByIdVersione", query = "SELECT f FROM File f WHERE f.idVersione = :idVersione")})
public class File implements Serializable {
@Transient
private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
private static final long serialVersionUID = 1L;
@Id
@Column(name = "id", nullable = false)
private Integer id;
@Column(name = "link_file")
private String linkFile;
@Column(name = "nome_file")
private String nomeFile;
@Column(name = "usato")
private Boolean usato;
@Column(name = "percorso_locale")
private String percorsoLocale;
@Column(name = "id_versione", nullable = false)
private int idVersione;
public File() {
}
public File(Integer id) {
this.id = id;
}
public File(Integer id, int idVersione) {
this.id = id;
this.idVersione = idVersione;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
Integer oldId = this.id;
this.id = id;
changeSupport.firePropertyChange("id", oldId, id);
}
public String getLinkFile() {
return linkFile;
}
public void setLinkFile(String linkFile) {
String oldLinkFile = this.linkFile;
this.linkFile = linkFile;
changeSupport.firePropertyChange("linkFile", oldLinkFile, linkFile);
}
public String getNomeFile() {
return nomeFile;
}
public void setNomeFile(String nomeFile) {
String oldNomeFile = this.nomeFile;
this.nomeFile = nomeFile;
changeSupport.firePropertyChange("nomeFile", oldNomeFile, nomeFile);
}
public Boolean getUsato() {
return usato;
}
public void setUsato(Boolean usato) {
Boolean oldUsato = this.usato;
this.usato = usato;
changeSupport.firePropertyChange("usato", oldUsato, usato);
}
public String getPercorsoLocale() {
return percorsoLocale;
}
public void setPercorsoLocale(String percorsoLocale) {
String oldPercorsoLocale = this.percorsoLocale;
this.percorsoLocale = percorsoLocale;
changeSupport.firePropertyChange("percorsoLocale", oldPercorsoLocale, percorsoLocale);
}
public int getIdVersione() {
return idVersione;
}
public void setIdVersione(int idVersione) {
int oldIdVersione = this.idVersione;
this.idVersione = idVersione;
changeSupport.firePropertyChange("idVersione", oldIdVersione, idVersione);
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof File)) {
return false;
}
File other = (File) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "it.polimi.swqtool.data.File[id=" + id + "]";
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
changeSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
changeSupport.addPropertyChangeListener(listener);
}
}
Se potreste essermi di aiuto ve ne sarei gratissimo!!!
Ciao.
Mainetz