在动态生成的复选框中添加ItemListener

时间:2017-01-25 08:14:24

标签: java swing checkbox

我正在Java Swing开发餐厅管理系统。我已经从数据库中动态显示了一个复选框列表,其中包含mainstock的名称(包含mainstock项目的表名)。我在循环期间在每个复选框上添加了一个ItemListener。

当我点击每个复选框时,它会弹出两个JFrame。为什么?我怎么能只弹出一个JFrame并插入所需的信息?

package test;

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

    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://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        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                   
}

0 个答案:

没有答案