SoftwareEnthusiast
SoftwareEnthusiast

Reputation: 271

Java Checkbox Action

I have a check box and when I create an Action script from the Netbeans' design, it creates a function like;

private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
            total=8.99f;
            xc = "XCheese";
            exTop++;
            calculateTotal(total);
            updateTextArea();

}

This works perfectly, but I want to set everything to zero when the jCheckBox1 is unchecked, if I uncheck it the way the code is now, no changes appear.

Upvotes: 0

Views: 7080

Answers (2)

edisonthk
edisonthk

Reputation: 1423

It is an sample of code. Hope it will help you.

private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {    
            if(checkBox.isSelected() ){
                   total=8.99f;
                   xc = "XCheese";
                   exTop++;
                   calculateTotal(total);
                   updateTextArea();
            }else{
                   // set everything zero here.
            }

}

Upvotes: 3

MadProgrammer
MadProgrammer

Reputation: 347194

Start by taking a look at How to Use Buttons, Check Boxes, and Radio Buttons

Basically, the ActionListener will be called when ever the check box is selected (checked) or unselected (unchecked). You need to check the state of the check box when ever the method is called.

Take a look at AbstractButton#isSelected which will tell you the (in this case) the checked state of the JCheckBox

Upvotes: 3

Related Questions