美好的一天!
我在将数据库的更新版本显示为JTable框架时出现问题...
我的JFrame中的所有内容都只是生成,我只是按照此链接中的说明操作:Binding JTable with MySQL
它正常工作,直到我为数据库的 UPDATES 添加了一个新的JFrame,当我点击按钮查看库存时(包含JTable的JFrame显示数据)在MySQL),它没有显示数据库的更新版本。
但是,关闭应用程序并运行ViewInventory JFrame后,它已经更新。
请帮助......这是生成的代码:
public class ViewInventory extends javax.swing.JFrame {
public ViewInventory() {
initComponents();
setLocation(300, 120);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
inventoryPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("inventoryPU").createEntityManager();
itemsQuery = java.beans.Beans.isDesignTime() ? null : inventoryPUEntityManager.createQuery("SELECT i FROM Items i");
itemsList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : itemsQuery.getResultList();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTable1.setAutoCreateRowSorter(true);
jTable1.setAlignmentX(1.0F);
jTable1.setAlignmentY(2.0F);
jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
jTable1.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, itemsList, jTable1);
org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${id}"));
columnBinding.setColumnName("Id");
columnBinding.setColumnClass(Integer.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${itemName}"));
columnBinding.setColumnName("Item Name");
columnBinding.setColumnClass(String.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("${QBox}"));
columnBinding.setColumnName("QBox");
columnBinding.setColumnClass(Integer.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${unitPriceBox}"));
columnBinding.setColumnName("Unit Price Box");
columnBinding.setColumnClass(Double.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${subTotalBox}"));
columnBinding.setColumnName("Sub Total Box");
columnBinding.setColumnClass(Double.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${QPc}"));
columnBinding.setColumnName("QPc");
columnBinding.setColumnClass(Integer.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${unitPricePc}"));
columnBinding.setColumnName("Unit Price Pc");
columnBinding.setColumnClass(Double.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${subTotalPc}"));
columnBinding.setColumnName("Sub Total Pc");
columnBinding.setColumnClass(Double.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${grandTotal}"));
columnBinding.setColumnName("Grand Total");
columnBinding.setColumnClass(Double.class);
bindingGroup.addBinding(jTableBinding);
jScrollPane1.setViewportView(jTable1);
jButton1.setText("Main Menu");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
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)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 514, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 329, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(14, 14, 14)
.addComponent(jButton1)
.addContainerGap())
);
bindingGroup.bind();
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
new Menu().setVisible(true);
setVisible(false);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ViewInventory().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.persistence.EntityManager inventoryPUEntityManager;
private java.util.List<inventory.Items> itemsList;
private javax.persistence.Query itemsQuery;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private org.jdesktop.beansbinding.BindingGroup bindingGroup;
// End of variables declaration
}
如果我在生成的代码中显示了所有内容,我很抱歉。
,这是我的EditItemDetails代码:
try {
Class.forName(Connect.DRIVER);
Connection con = DriverManager.getConnection(Connect.CONNECTION_STRING,
Connect.USERNAME, Connect.PASSWORD);
Statement stat = con.createStatement();
ResultSet rs = stat.executeQuery("SELECT id, category,"
+ "q_box, unit_price_box, sub_total_box, q_pc, "
+ "unit_price_pc, sub_total_pc, grand_total from items WHERE "
+ "item_name = '" + item_name_combobox.getSelectedItem() + "';");
String category;
int q_box, q_pc;
double unit_price_box, sub_total_box, unit_price_pc, sub_total_pc,
grand_total;
if (rs.next()) {
q_box = rs.getInt("q_box");
category = rs.getString("category");
unit_price_box = rs.getDouble("unit_price_box");
sub_total_box = rs.getDouble("sub_total_box");
q_pc = rs.getInt("q_pc");
unit_price_pc = rs.getDouble("unit_price_pc");
sub_total_pc = rs.getDouble("sub_total_pc");
grand_total = rs.getDouble("grand_total");
double new_sub_total_box = q_box * Integer.parseInt(new_unit_price_box_txtfld.getText());
double new_sub_total_pc = q_pc * Integer.parseInt(new_unit_price_pc_txtfld.getText());
grand_total = new_sub_total_box + new_sub_total_pc;
stat.executeUpdate("UPDATE items SET unit_price_box = " + new_unit_price_box_txtfld.getText()
+ ", unit_price_pc = " + new_unit_price_pc_txtfld.getText()
+ ", sub_total_box = " + new_sub_total_box
+ ", sub_total_pc = "+ new_sub_total_pc
+ ", grand_total = "+ grand_total
+ " WHERE item_name = '"
+ item_name_combobox.getSelectedItem() + "';");
sub_total_box_txtfld.setText(Double.toString(new_sub_total_box));
sub_total_pc_txtfld.setText(Double.toString(new_sub_total_pc));
grand_total_txtfld.setText(Double.toString(grand_total));
lblmsg.setText("Record Saved!");
con.close();
}
} catch (Exception e) {
lblmsg.setText("Error: "+ e.getMessage());
System.out.println("Error: "+ e.getMessage());
}
答案 0 :(得分:0)
我个人不喜欢Netbeans gui设计师有很多原因。它非常适合原型设计和小型项目,对于大型项目来说非常糟糕,并且可以重新进行维护。但这又是另一次咆哮。
出现问题的是您从JPA查询中获取静态列表。您需要让GUI在编辑事件后重新运行查询以获取更新列表。我在gui设计师中使用这种方法的方法是将集合更改为&#34;自定义创建代码&#34;中的ObservableList:
someList = Beans.isDesignTime() ? java.util.Collections.emptyList() : ObservableCollections.observableList(new ArrayList<Something>());
然后当我有一个更新JTable内容的gui事件时,我做了类似的事情:
someList.clear();
someList.add(query.getResultList());
我这样做是因为我通过EJB远程会话bean获取列表,我很久以前就在netbeans 6.5或7中做过这个(我最后一次使用GUI设计器来创建一个新的形成)。显然现在有一个&#34; modifiableWrapper &#34;和&#34; 可观察&#34;您可以设置查询结果。这可能会做你需要的,否则就像上面的手动方式那样。