这里有Java和netbeans noob。我有一个Jtable绑定到我的Javadb。我可以使用JPAController与数据库进行交互。我可以使用JComboBox和getSelectedItem()方法轻松访问该对象,并将其强制转换为我的数据库对象以进行编辑或销毁。例如:
private void deleteInfoButtonActionPerformed(java.awt.event.ActionEvent evt) {
Customers deleteInfo = (Customers) DeleteInfoComboBox.getSelectedItem();
if (deleteInfo != null) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("CustomersPU");
CustomersJpaController cjc = new CustomersJpaController(emf);
try {
cjc.destroy(deleteInfo.getId());
} catch (NonexistentEntityException ex) {
Logger.getLogger(DeleteInformant.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
是否有类似的方法使用Jtable或Jlist返回对象?我曾尝试使用getSelectedRow()方法,但这只返回与Jtable中的行相关的整数,该整数与数据库中的ID不同。我看到的唯一返回对象的方法是getValueAt(),但它返回指定单元格的对象。对于Jlist,getSelectedValuesList()只返回一个字符串向量。 getSelectedValues()似乎做我想要的但折旧。从其他帖子(Here),似乎我可能需要创建一个自定义表模型,但该示例似乎没有考虑使用netbeans自动生成JPAController。
提前感谢您的帮助!
更新:这是自动生成的代码,使用所请求的值填充Jlist:
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
CustomersPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("CustomersPU").createEntityManager();
customerssQuery = java.beans.Beans.isDesignTime() ? null : CustomersPUEntityManager.createQuery("SELECT i FROM Customers i");
customersList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : customersQuery.getResultList();
jScrollPane1 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList<>();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
org.jdesktop.swingbinding.JListBinding jListBinding = org.jdesktop.swingbinding.SwingBindings.createJListBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, customersList, jList1);
bindingGroup.addBinding(jListBinding);
jScrollPane1.setViewportView(jList1);
jButton1.setText("jButton1");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(34, 34, 34)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(54, 54, 54)
.addComponent(jButton1)
.addContainerGap(89, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(65, 65, 65)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(130, 130, 130)
.addComponent(jButton1)))
.addContainerGap(105, Short.MAX_VALUE))
);
bindingGroup.bind();
pack();
}// </editor-fold>
答案 0 :(得分:0)
NetBeans自动生成一个名为customersList的变量,该变量包含填充Jtable或Jlist的Customers对象列表。此变量可用于通过将get()方法与所选项的索引一起访问所选对象。
对于Jtable:
Customers selectedCustomer = customersList.get(jTable1.getSelectedRow());
对于具有多个选择的Jlist,可以生成一个Customers对象数组,可以使用JPAController对其进行迭代和操作:
Customers[] selectedCustomers = new Customers [jList1.getSelectedIndices().length];
for (int i = 0; i < jList1.getSelectedIndices().length; i++) {
selectedCustomers[i] = customersList.get(jList1.getSelectedIndices()[i]);
}
仅当Jlist或Jtable的顺序不变时才有效。如果您希望能够对项目进行排序,则可以遍历列表以查找正确的对象。这就是我在Jtable中使用我的表的ID列实现它的方法:
public static int idColumn = 0;
for (Customers i : customersList) {
if (i.getId() == jTable1.getValueAt(jTable1.getSelectedRow(), idColumn)) {
Customers selectedCustomer = i;
}
}