mercredi 25 janvier 2017

adding itemlistener in dynamically generated checkboxes

I am developing restaurant management system in java swing and yet, i have displayed a list of checkboxes dynamically from databases which contains the names of mainstock(table names which contains mainstock items) . i have add itemlistener on each checkbox during the loop. when i clicked each checkbox, it popups two jframe ,,,,why? how could i popup only one jframe and insert required information? plz help,,i m new to swing

enter code here package test;

public class test extends javax.swing.JFrame { public static ArrayList list = new ArrayList();

public test() {
    initComponents();

    Connection con = (Connection) dbconnection.makeconnection();
    String sql="select * from mainstock";
     try{
            Statement st = con.createStatement();
          ResultSet rs =  st.executeQuery(sql); 
          while(rs.next()){
              String s2=rs.getString("stock_name");
             JCheckBox cb = new JCheckBox("New CheckBox");
              cb.setText(s2);
                cb.setVisible(true);
                   cb.addItemListener(new ItemListener() {

                  @Override
                  public void itemStateChanged(ItemEvent e) {


                      String name = cb.getActionCommand();
                      System.out.println(name);
                      JFrame frame = new JFrame(name);
                      int stock_id= rmsdao.givestockid(name);
                      System.out.println(stock_id);
                       String estimatedplate = JOptionPane.showInputDialog(frame, "Enter the estimated  plate of 1 kg"+name);

                        if(estimatedplate!=null){
                            list.add(new mdetails(stock_id,estimatedplate));
                        }
                        else{
                            cb.setSelected(false);
                        }
                  }
              }); 
                    jPanel1.add(cb);
                    jPanel1.validate();
          }
          con.close();
        }
        catch(Exception e){
            System.out.println("not retrieving"+e);
        }

}


@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setResizable(false);

    jButton1.setText("jButton1");
    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(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(65, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 252, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(23, 23, 23))
        .addGroup(layout.createSequentialGroup()
            .addGap(129, 129, 129)
            .addComponent(jButton1)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(37, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(50, 50, 50)
            .addComponent(jButton1)
            .addContainerGap())
    );

    pack();
}// </editor-fold>                        

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
   for (int a = 0; a < list.size(); a++) {
                mdetails item = list.get(a);
                System.out.println(item.getId());
            //    rmsdao.insert_menu_details(item.getId(),getitemid,item.getEstimate_plate());
            }
}                                        


public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://ift.tt/1cNmMj1 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new test().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;
// End of variables declaration                   

}




Aucun commentaire:

Enregistrer un commentaire