这是我的问题。我创建了一个JForm,它应该有助于在数据库中插入新对象“Analyze”,而那些“Analyze”对象需要创建一个“Lot”对象id。
所以我创建了一个允许我选择“Lot”id的JCombobox。组合框渲染器允许我看到值,该值不为null,但是当我使用提交按钮时返回的值为null ...
以下是错误代码:
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'id_lot' cannot be null
Error Code: 1048
Call: INSERT INTO analyses (date_validite, id_lot) VALUES (?, ?)
bind => [2015-03-10, null]
Query: InsertObjectQuery(analysesmanager.Analyses[idAnalyse=null])
我知道为什么会出现这个错误...我不允许用户使用null id_lot添加Analyze。这个是正常的。异常部分是我的JForm发送空值......
这是JForm代码:
package analysesmanager;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
/**
*
* @author m.desmidt
*/
public class NewAnalyse extends javax.swing.JDialog {
protected Analyses currentRecord;
/**
* Get the value of currentRecord
*
* @return the value of currentRecord
*/
public Analyses getCurrentRecord() {
return currentRecord;
}
/**
* Set the value of currentRecord
*
* @param currentRecord new value of currentRecord
*/
public void setCurrentRecord(Analyses currentRecord) {
Analyses oldRecord = this.currentRecord;
this.currentRecord = currentRecord;
propertyChangeSupport.firePropertyChange("currentRecord", oldRecord, currentRecord);
}
private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
protected boolean AnalyseConfirmed;
/**
* Get the value of AnalyseConfirmed
*
* @return the value of AnalyseConfirmed
*/
public boolean isAnalyseConfirmed() {
return AnalyseConfirmed;
}
/**
* Set the value of AnalyseConfirmed
*
* @param AnalyseConfirmed new value of AnalyseConfirmed
*/
public void setAnalyseConfirmed(boolean AnalyseConfirmed) {
this.AnalyseConfirmed = AnalyseConfirmed;
}
/**
* Add PropertyChangeListener.
*
* @param listener
*/
@Override
public void addPropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(listener);
}
/**
* Remove PropertyChangeListener.
*
* @param listener
*/
@Override
public void removePropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(listener);
}
/** Creates new form NewAnalyse */
public NewAnalyse(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
}
/** 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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
analyses_2PUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("analyses_2PU").createEntityManager();
lotsQuery = java.beans.Beans.isDesignTime() ? null : analyses_2PUEntityManager.createQuery("SELECT l FROM Lots l");
lotsList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : lotsQuery.getResultList();
dateVerifier1 = new analysesmanager.DateVerifier();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jComboBox1 = new javax.swing.JComboBox();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jFormattedTextField1 = new javax.swing.JFormattedTextField();
lotsRenderer1 = new analysesmanager.LotsRenderer();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(analysesmanager.AnalysesManagerApp.class).getContext().getResourceMap(NewAnalyse.class);
setTitle(resourceMap.getString("Form.title")); // NOI18N
setModal(true);
setName("Form"); // NOI18N
jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
jLabel1.setName("jLabel1"); // NOI18N
jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
jLabel2.setName("jLabel2"); // NOI18N
jComboBox1.setName("jComboBox1"); // NOI18N
jComboBox1.setRenderer(lotsRenderer1);
org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, lotsList, jComboBox1);
bindingGroup.addBinding(jComboBoxBinding);
org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${currentRecord.lots.idLot}"), jComboBox1, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
bindingGroup.addBinding(binding);
jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
jButton1.setName("jButton1"); // NOI18N
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CancelAnalyse(evt);
}
});
jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
jButton2.setName("jButton2"); // NOI18N
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SaveAnalyse(evt);
}
});
jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
jLabel3.setName("jLabel3"); // NOI18N
jTextField2.setName("jTextField2"); // NOI18N
binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${currentRecord.idAnalyse}"), jTextField2, org.jdesktop.beansbinding.BeanProperty.create("text"));
bindingGroup.addBinding(binding);
jFormattedTextField1.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter()));
jFormattedTextField1.setInputVerifier(dateVerifier1);
jFormattedTextField1.setName("jFormattedTextField1"); // NOI18N
binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${currentRecord.dateValidite}"), jFormattedTextField1, org.jdesktop.beansbinding.BeanProperty.create("value"));
bindingGroup.addBinding(binding);
lotsRenderer1.setText(resourceMap.getString("lotsRenderer1.text")); // NOI18N
lotsRenderer1.setName("lotsRenderer1"); // NOI18N
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel3)
.addComponent(jLabel2))
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jFormattedTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE)
.addComponent(jTextField2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE)
.addComponent(jComboBox1, javax.swing.GroupLayout.Alignment.LEADING, 0, 324, Short.MAX_VALUE)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(lotsRenderer1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 189, Short.MAX_VALUE)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jFormattedTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2)
.addComponent(lotsRenderer1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(22, Short.MAX_VALUE))
);
bindingGroup.bind();
pack();
}// </editor-fold>
private void SaveAnalyse(java.awt.event.ActionEvent evt) {
setAnalyseConfirmed(true);
setVisible(false);
}
private void CancelAnalyse(java.awt.event.ActionEvent evt) {
setAnalyseConfirmed(false);
setVisible(false);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
NewAnalyse dialog = new NewAnalyse(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.persistence.EntityManager analyses_2PUEntityManager;
private analysesmanager.DateVerifier dateVerifier1;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JFormattedTextField jFormattedTextField1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField jTextField2;
private java.util.List<analysesmanager.Lots> lotsList;
private javax.persistence.Query lotsQuery;
private analysesmanager.LotsRenderer lotsRenderer1;
private org.jdesktop.beansbinding.BindingGroup bindingGroup;
// End of variables declaration
}
以下是关于我的JCombobox如何绑定的screen。
我做错了什么? 火焰接受,只要它有用......
答案 0 :(得分:0)
我设法使用一个小的解决方法...而不是选择'lots.idLot',我选择'lot',然后找到它的'idlot'。
有效。不是我一开始想要的方式,但它有效。