Frage

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.

War es hilfreich?

Lösung

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.
            }

}

Andere Tipps

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top