如何在jcombobox中选择项目时附加动作

时间:2014-02-20 15:45:36

标签: java swing netbeans jcombobox

我正在使用客户的名字搜索旧发票。通常我在jcombo框中选择项目并按下按钮以执行操作。它运作正常。我想知道当我从jcombo框中选择项目时,有什么方法可以加载动作。 enter image description here



另外我想知道如何刷新jtable。请善意帮助我..谢谢。
这是我写入按钮动作的源代码。

           private void btnSearchCustomerActionPerformed(java.awt.event.ActionEvent evt) {                                                  
      public void loadOldInvoiceByCustomer(JComboBox comboCustomer, JTable jtable2) {

    try {
        ResultSet rs1 = db.getData("SELECT cuid FROM customer WHERE cuname='" + comboCustomer.getSelectedItem().toString().trim() + "' ");
        while (rs1.next()) {
            try {
                ResultSet rs2 = db.getData("SELECT DISTINCT inid FROM invoice WHERE cuid=' " + rs1.getInt("cuid") + " ' ");
               while (rs2.next()) {
                    ResultSet rs3 = db.getData("SELECT DISTINCT isdate FROM  indetails WHERE inid='" + rs2.getInt("inid") + "'");
                    try {
                        while (rs3.next()) {
                            Vector v = new Vector();
                            DefaultTableModel df = (DefaultTableModel) jtable2.getModel();
                            v.add(rs2.getInt("inid"));
                            v.add(comboCustomer.getSelectedItem().toString());
                            v.add(rs3.getString("isdate"));
                            df.addRow(v);
                        }
                    } catch (Exception e) {

                        JOptionPane.showMessageDialog(null, this.getClass().getName() + " " + "rs3" + e);
                    }
                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, this.getClass().getName() + " " + "rs2" + e);
            }
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, this.getClass().getName() + " " + "rs1" + e);
    }
}
}

2 个答案:

答案 0 :(得分:0)

对于JComboBox,你必须像这样调用actionListener的方法:

combo.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                JOptionPane.showMessageDialog(null, "selected: " + combo.getItemAt(combo.getSelectedIndex()));
            }

        });

答案 1 :(得分:0)

当Combobox项目更改时,您可以添加ItemListener,该事件被称为

JCombobox.addItemListener(new ItemListener(){
     public void itemStateChanged(ItemEvent e){
         System.out.println(e.getItem());
     }
});