Question

I'm searching old invoices using customer's name. Usually I select item in jcombo box and press the button to happen the action. It is working properly. I want to know is there any way we can load the action when I selecting the item from jcombo box. enter image description here



Also I want to know how to refresh the jtable. Please be kind to help me.. thank you.
This is the source code I write to button's action.

           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);
    }
}
}
Was it helpful?

Solution

for JComboBox you must call the actionListener's method like this:

combo.addActionListener(new ActionListener(){

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

        });

OTHER TIPS

You can add ItemListener when Combobox item changed the event is called

JCombobox.addItemListener(new ItemListener(){
     public void itemStateChanged(ItemEvent e){
         System.out.println(e.getItem());
     }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top