以下是netbeans在使用netbeans gui编辑器绑定jtable时生成的代码。
serverDetailsQuery = java.beans.Beans.isDesignTime() ? null : F1SoftSMSCPUEntityManager.createQuery("SELECT s FROM ServerDetails s ");
我通过编辑改变了查询
SELECT s FROM ServerDetails s order by id desc
这很好用。现在我想通过ShortCode将查询参数传递给过滤记录,如:
SELECT s FROM ServerDetails s where s.shortCode : filterShortCode order by id desc
短代码是文本字段,如果用户键入shortCode并单击按钮,则会有一个按钮来过滤记录。
相关代码
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
F1SoftSMSCPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("F1SoftSMSCPU").createEntityManager();
serverDetailsQuery = java.beans.Beans.isDesignTime() ? null : F1SoftSMSCPUEntityManager.createQuery("SELECT s FROM ServerDetails s order by id desc");
serverDetailsList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : serverDetailsQuery.getResultList();
jPanel1 = new javax.swing.JPanel();
serverDetailsPanel = new javax.swing.JPanel();
bindButton = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
bindingDetailsTable = new javax.swing.JTable();
blockButtonToBindingDetails = new javax.swing.JButton();
smscBindingDetailsFilterPanel = new javax.swing.JPanel();
filterByShortCodeTextField = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
smscBindingFilterButton = new javax.swing.JButton();
}// Variables declaration - do not modify
private javax.persistence.EntityManager F1SoftSMSCPUEntityManager;
private javax.swing.JButton bindButton;
private javax.swing.JTable bindingDetailsTable;
private javax.swing.JButton blockButtonToBindingDetails;
private javax.swing.JTextField filterByShortCodeTextField;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private java.util.List<com.f1soft.SMSC.entities.ServerDetails> serverDetailsList;
private javax.swing.JPanel serverDetailsPanel;
private javax.persistence.Query serverDetailsQuery;
private javax.swing.JPanel smscBindingDetailsFilterPanel;
private javax.swing.JButton smscBindingFilterButton;
private org.jdesktop.beansbinding.BindingGroup bindingGroup;
// End of variables declaration
感谢并愿意尽快得到专家的回复。
答案 0 :(得分:2)
以下是将参数传递给查询的方法:
Query q = em.createQuery(jpql);
q.setParameter("filterShortCode", theParameterValue);
编辑:
上面的代码应放在一个监听器方法中,单击该按钮时调用。它不应该放在initComponents()
中,因为很明显,当调用initComponents()
时,用户还没有填写文本并点击了按钮:
smscBindingFilterButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String theParameterValue = filterByShortCodeTextField.getText();
// TODO create and execute the query
}
}
考虑到你的问题,我认为你对Swing和一般事件都不熟悉。我建议你忘记NetBeans wysiwyg编辑器和练习,简单的例子不涉及绑定和JPA。请关注Swing tutorial,特别是the part about events。