How to code if a checkbox is unchecked in java

感情迁移 提交于 2019-12-13 07:58:41

问题


Currently, I'm developing a Restaurant management system in java. when I check a checkbox it will add money to the total amount, all I want to know that if I uncheck that checked checkbox again then it will subtract that added amount again. following is my checkbox code

private void jcbWPizzaMouseClicked(java.awt.event.MouseEvent evt) {                                       
    double cMeal = Double.parseDouble(jlblCostOfMeal.getText());
    double WavePizza = Double.parseDouble(jtxtWP.getText());
    double iWavePizza = 350;

        if (jcbWPizza.isSelected()) {
        i[1] = (WavePizza * iWavePizza) + cMeal;
        String pMeal = String.format("%.2f", i[1]);
        jlblCostOfMeal.setText(pMeal);
    }
} 

回答1:


if you are using JCheckBox there is a method boolean isSelected() so to verify whether the JCheckBox is selected you can try :

if (checkbox.isSelected()) {
// selected, do something...
} else {
// un-selected, do something else...
}



回答2:


You want to change the value of total amount based on the object jcbWPizza's selected stated. you can add ItemListener to the checkbox

jcbWPizza.addItemListener(new ItemListener() {
 @Override public void itemStateChanged(ItemEvent e) {
  if (e.getStateChange() == ItemEvent.SELECTED) {
   //checkbox has been selected //do selected action... 
  } else {
   //checkbox has been deselected //do deselected action...   
  };
 }
})



回答3:


Your question raises a number of issues that you might not have thought about.

Firstly, by relying on a MouseEvent, you will not capture those times when the checkbox changes state due to some other reason (for example, by code, or if the user uses the keyboard to change selection). Adding an ItemListener would be a more generation approach.

In the future, there might be a number of other things that affect the price. Having the code that updates the price hidden in this checkbox listener seems like the wrong approach. What would be better is to add a listener that just calls a general "update the displayed price" method. Within that method, you could then check the state of each of the widgets that will affect the final price and calculate accordingly. It then doesn't matter if the checkbox is or was hidden or not, as each time it is asked to update, it will calculate the total from scratch.

Finally, bear in mind that Swing is not thread-safe. Whilst a single Thread will call your listeners, there is no guarantee that only a single Thread will be calling your "recalculate the price" method. Ensure that if more than one Thread should call your code at the same time, you don't end up getting your state out of sync. Avoiding a "the checkbox has been toggled, so add or subtract" logic is a good idea once again, as it adds additional state that needs to be kept in sync.



来源:https://stackoverflow.com/questions/49853232/how-to-code-if-a-checkbox-is-unchecked-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!