0

I am coding a game in java swing in which the user has to click on check boxes. The number of check boxes selected increases with each selection but for some reason also increases whenever you unselect the check box.

choice[0]=5; // assigns  a finite number for user's choice to improve program efficiency 
        select++;              
        jTextField2.setText(String.valueOf(select));  // prints the number of  checkboxes selected
        if (jCheckBox1.setSelected(true))    
        {
            select--;
        }
        if(select==5)          // disables all check boxes when 5 are 
                                    selected
        {
           jCheckBox1.setEnabled(false);
           jCheckBox2.setEnabled(false);
           jCheckBox3.setEnabled(false);
           jCheckBox4.setEnabled(false);
           jCheckBox5.setEnabled(false);
           jCheckBox6.setEnabled(false);
           jCheckBox7.setEnabled(false);
           jCheckBox8.setEnabled(false);
           jCheckBox9.setEnabled(false);
           jCheckBox10.setEnabled(false);
           jCheckBox11.setEnabled(false);
           jCheckBox12.setEnabled(false);
           jCheckBox13.setEnabled(false);
           jCheckBox14.setEnabled(false);
           jCheckBox15.setEnabled(false);
           jLabel6.setText("Great! Now click on the 'Found you!' button to continue. ");  //restricts user from selecting more than 5 boxes
                                                                                              //and prompts them to click on 'Found you ' button.
        }

The line of code if (jCheckBox1.setSelected(true)) gives me the error "void cannot be converted to boolean". Can someone please help me improve the code in a way that decreases the value of "select" whenever a checkbox is unselected.

Thanks.

Locke
  • 5
  • 3
  • 1
    `if (jCheckBox1.isSelected())` ... ? Also, you really, really want to learn how to use arrays – MadProgrammer Jan 25 '18 at 03:08
  • @MadProgrammer, yeah I should. Thanks for the help tho, again. – Locke Jan 25 '18 at 03:11
  • You should also have a read through [How to use buttons](https://docs.oracle.com/javase/tutorial/uiswing/components/button.html) which would have provided you with examples that would have helped you solve your problem. SO is not a substitute for good tutorials – MadProgrammer Jan 25 '18 at 03:18

1 Answers1

0

Use the following:

if (jCheckBox1.isSelected())

setSelected(..) sets the value of the checkbox. It doesn't obtain it.

Daniel Centore
  • 3,220
  • 1
  • 18
  • 39