0

I have records in database that I added on my program within jcheckbox but my problem here is that i'm not really sure how to add itemListener to those jcheckbox. I created an array of jcheckbox. What exactly i want to do is once that checkbox was selected, others will be disabled. So i'll be needing the itemStateChanged event for this one.

Please any help.

public void my_form(){
     JCheckBox jcb = new JCheckBox [number];
     int counter = 0;         
     PreparedStatement pst1;
     ResultSet rest1; 
try{             
         pst1 = con.prepareStatement("select * from my_info");
         rest1 = pst1.executeQuery();

         while(rest1.next()){

             String nameLast = rest1.getString("cand_nameLast");
             String nameFirst = rest1.getString("cand_nameFirst");
             jcb[counter] = new JCheckBox(nameLast +", "+ nameFirst);

             //here i guess itemListener should be added. I'm not really sure
             //how it should be.

             jPanel6.add(jcb[counter]);
             jPanel6.revalidate();
             jPanel6.repaint();
             counter ++;                 
         }             
     }catch(Exception e){
         JOptionPane.showMessageDialog(null, e.getMessage());
     }
 }
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
  • 1
    Start by doing some research into ButtonGroup. Simply put, create an instance of ItemListener (either using a anonymous class or inner class) and simply call addItemListener on the JCheckBox ... Although to be frank a ActionListener would be more practical – MadProgrammer Feb 19 '16 at 03:45
  • *What exactly I want to do is once that checkbox was selected, others will be disabled.* Sounds like a job for JRadioButtons. – Gilbert Le Blanc Feb 21 '16 at 07:39

0 Answers0