简体   繁体   中英

How to get value from dynamic JCheckBox?

I created dynamic JCheckBox , and I don't know how to get their value when I check it.

When I click the checkbox I want to get value and put them to SQL query.

like : query = select checkbox1, checkbox2 from table

This is my dynamic checkbox code:

roll[K] = new JCheckBox();
        roll[K].setText(metaData.getColumnLabel(columnIndex));
        roll[K].setBounds(X,Y,150,30);
        Y = Y+30;
        Rectangle r = jPanel3.getBounds();
        int h=r.height;
        if (Y>=h-50){
            Y=0;
            X=X+200;
       }
   jPanel3.add(roll[K]);

You can set checkbox item listener like this

roll[K].setActionCommand(Integer.toString(k));
  roll[K].addItemListener(new ItemListener() {

      public void itemStateChanged(ItemEvent e) {
        //this is not necessary and depends upon you, how do u want 
        // use this
        int id = Integer.parseInt(Integer.getSource().getActionCommand())
         //can apply switch case on ID to take appropriate action based on ID
         if (e.getStateChange() == 1) 
               System.out.println("Checked");
         else
               System.out.println("un-Checked");

      }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM