我的表单(Jpanel)中有一个 jtable,它使用 jpa 通过 jtableBinding 绑定到 DB。(通过netbeans自动生成)
我通过在实体类@transient 中创建对应的字段,在jtable 中添加了一个复选框。这样我就不需要数据库表中此复选框的列。
我现在面临的问题是该复选框不可编辑。
我的实体类
@Entity
@Table(name = "PRODUCT")
@NamedQueries({@NamedQuery(name = "Product.findAll",
query = "SELECT p FROM Product p"),
@NamedQuery(name ="Product.findByProductId",
query = "SELECT p FROM Product p WHERE p.productId = :productId"),
@NamedQuery(name = "Product.findByPurchaseCost",
query = "SELECT p FROM Product p WHERE p.purchaseCost =
:purchaseCost"), @NamedQuery
(name = "Product.findByAvailable",
query = "SELECT p FROM Product p WHERE p.available = :available"),
@NamedQuery(name = "Product.findByDescription",
query = "SELECT p FROM Product p WHERE p.description = :description"),
@NamedQuery(name = "Product.findByMrp",
query = "SELECT p FROM Product p WHERE p.mrp = :mrp"),
@NamedQuery(name = "Product.findByProductName",
query = "SELECT p FROM Product p WHERE p.productName = :productName"),
@NamedQuery(name = "Product.findByManufacturer",
query = "SELECT p FROM Product p WHERE p.manufacturer = :manufacturer"),
@NamedQuery(name = "Product.findByProductCode",
query = "SELECT p FROM Product p WHERE p.productCode = :productCode"),
@NamedQuery(name = "Product.findByCategory",
query = "SELECT p FROM Product p WHERE p.category = :category")})
.
public class Product implements Serializable {
@Transient
private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "PRODUCT_ID")
private Integer productId;
@Column(name = "PURCHASE_COST")
private Long purchaseCost;
@Column(name = "AVAILABLE")
private String available;
@Column(name = "DESCRIPTION")
private String description;
@Column(name = "MRP")
private BigDecimal mrp;
@Column(name = "PRODUCT_NAME")
private String productName;
@Column(name = "MANUFACTURER")
private String manufacturer;
@Column(name = "PRODUCT_CODE")
private String productCode;
@Column(name = "CATEGORY")
private String category;
@Transient
private String selected;
public String isSelected() {
return selected;
}
public void setSelected(String selected) {
this.selected = selected;
}
public Product() {
}
public Product(Integer productId) {
this.productId = productId;
}
public Integer getProductId() {
return productId;
}
public void setProductId(Integer productId) {
Integer oldProductId = this.productId;
this.productId = productId;
changeSupport.firePropertyChange("productId", oldProductId, productId);
}
public Long getPurchaseCost() {
return purchaseCost;
}
public void setPurchaseCost(Long purchaseCost) {
Long oldPurchaseCost = this.purchaseCost;
this.purchaseCost = purchaseCost;
changeSupport.firePropertyChange("purchaseCost", oldPurchaseCost, purchaseCost);
}
public String getAvailable() {
return available;
}
public void setAvailable(String available) {
String oldAvailable = this.available;
this.available = available;
changeSupport.firePropertyChange("available", oldAvailable, available);
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
String oldDescription = this.description;
this.description = description;
changeSupport.firePropertyChange("description", oldDescription, description);
}
public BigDecimal getMrp() {
return mrp;
}
public void setMrp(BigDecimal mrp) {
BigDecimal oldMrp = this.mrp;
this.mrp = mrp;
changeSupport.firePropertyChange("mrp", oldMrp, mrp);
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
String oldProductName = this.productName;
this.productName = productName;
changeSupport.firePropertyChange("productName", oldProductName, productName);
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
String oldManufacturer = this.manufacturer;
this.manufacturer = manufacturer;
changeSupport.firePropertyChange("manufacturer", oldManufacturer, manufacturer);
}
public String getProductCode() {
return productCode;
}
public void setProductCode(String productCode) {
String oldProductCode = this.productCode;
this.productCode = productCode;
changeSupport.firePropertyChange("productCode", oldProductCode, productCode);
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
String oldCategory = this.category;
this.category = category;
changeSupport.firePropertyChange("category", oldCategory, category);
}
@Override
public int hashCode() {
int hash = 0;
hash += (productId != null ? productId.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 Product)) {
return false;
}
Product other = (Product) object;
if ((this.productId == null && other.productId != null) || (this.productId != null && !this.productId.equals(other.productId))) {
return false;
}
return true;
}
@Override
public String toString() {
return "arabna.inventory.dao.Product[productId=" + productId + "]";
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
changeSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
changeSupport.removePropertyChangeListener(listener);
}
}
我生成的 UI 代码
private void initComponents() {
bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
ArabanaTradingPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("ArabanaTradingPU").createEntityManager();
productQuery = java.beans.Beans.isDesignTime() ? null : ArabanaTradingPUEntityManager.createQuery("SELECT p FROM Product p");
productList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : productQuery.getResultList();
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
addItemBtn = new javax.swing.JButton();
searchBtn = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
saveItemBtn = new javax.swing.JButton();
deleteBtn = new javax.swing.JButton();
EditBtn = new javax.swing.JButton();
jComboBox1 = new javax.swing.JComboBox();
setBorder(javax.swing.BorderFactory.createEtchedBorder());
setMaximumSize(new java.awt.Dimension(810, 710));
setMinimumSize(new java.awt.Dimension(805, 705));
setPreferredSize(new java.awt.Dimension(805, 705));
jPanel1.setMaximumSize(new java.awt.Dimension(800, 700));
jPanel1.setMinimumSize(new java.awt.Dimension(800, 700));
jPanel1.setPreferredSize(new java.awt.Dimension(800, 700));
jPanel1.addAncestorListener(new javax.swing.event.AncestorListener() {
public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
}
public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
jPanel1formAncestorAdded(evt);
}
public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
}
});
jScrollPane1.setMaximumSize(new java.awt.Dimension(730, 310));
jScrollPane1.setMinimumSize(new java.awt.Dimension(720, 300));
jScrollPane1.setPreferredSize(new java.awt.Dimension(715, 315));
jTable1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jTable1.setColumnSelectionAllowed(true);
jTable1.setMaximumSize(new java.awt.Dimension(720, 300));
jTable1.setMinimumSize(new java.awt.Dimension(710, 290));
jTable1.setPreferredSize(new java.awt.Dimension(715, 295));
jTable1.setRowHeight(35);
jTable1.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
jTable1.getTableHeader().setReorderingAllowed(false);
org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, productList, jTable1, "itemTableBinding");
org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${selected}"));
columnBinding.setColumnName("Selector");
columnBinding.setColumnClass(Boolean.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${available}"));
columnBinding.setColumnName("Available");
columnBinding.setColumnClass(Boolean.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${category}"));
columnBinding.setColumnName("Category");
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("${manufacturer}"));
columnBinding.setColumnName("Manufacturer");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${mrp}"));
columnBinding.setColumnName("Mrp");
columnBinding.setColumnClass(java.math.BigDecimal.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${productCode}"));
columnBinding.setColumnName("Product Code");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${productId}"));
columnBinding.setColumnName("Product Id");
columnBinding.setColumnClass(Integer.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${productName}"));
columnBinding.setColumnName("Product Name");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${purchaseCost}"));
columnBinding.setColumnName("Purchase Cost");
columnBinding.setColumnClass(Long.class);
bindingGroup.addBinding(jTableBinding);
jTableBinding.bind();
jScrollPane1.setViewportView(jTable1);
jTable1.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
addItemBtn.setText("ADD ITEM");
addItemBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addItemBtnActionPerformed(evt);
}
});
searchBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/arabna/inventory/ui/images/search.jpg"))); // NOI18N
searchBtn.setText("SEARCH");
searchBtn.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
searchBtn.setIconTextGap(10);
searchBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
searchBtnActionPerformed(evt);
}
});
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("ITEM MASTER");
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
saveItemBtn.setText("SAVE ITEM");
saveItemBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveItemBtnActionPerformed(evt);
}
});
deleteBtn.setText("DELETE ITEM");
deleteBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
deleteBtnActionPerformed(evt);
}
});
EditBtn.setText("EDIT");
EditBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
EditBtnActionPerformed(evt);
}
});
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Manufacturer", "product" }));
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(280, 280, 280)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(411, 411, 411))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(addItemBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(saveItemBtn)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(deleteBtn)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(EditBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jComboBox1, 0, 208, Short.MAX_VALUE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(searchBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jComboBox1, jTextField1});
jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {EditBtn, addItemBtn, deleteBtn, saveItemBtn});
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(11, 11, 11)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(addItemBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(saveItemBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(deleteBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(EditBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(searchBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(43, 43, 43)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(230, Short.MAX_VALUE))
);
jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {EditBtn, addItemBtn, deleteBtn, saveItemBtn, searchBtn});
jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jComboBox1, jTextField1});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 820, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap()))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 491, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap()))
);
bindingGroup.bind();
}// </editor-fold>