JavaFX - Is there a way to loop a checkbox?

Viewed 156

I am wondering if there's a way to loop a checkbox instead of using bunch of IF loops.

My concern here is: Is there a way to loop this and why I cannot use Else If when conditioning checkbox.

Here is my code:

@FXML
private CheckBox cb1;

@FXML
private CheckBox cb2;

@FXML
private CheckBox cb3;

@FXML
private CheckBox cb4;


public void checkEvent(ActionEvent event) {
    
    int price = 0;
    
    String message = "";
    
    if (cb1.isSelected()) {
        price = 150;
        message += cb1.getText() + " Price: " + price  + "\n";
    }
    
    if (cb2.isSelected()) {
        price = 200;
        message += cb2.getText() + " Price: " + price + "\n";
    }
    
    if (cb3.isSelected()) {
        price = 100;
        message += cb3.getText() + " Price: " + price + "\n";
    }
    
    if (cb4.isSelected()) {
        price = 200;
        message += cb4.getText() + " Price: " + price +"\n";
    }
    
                    
    lbllist.setText(message);
    
}

Output:

Let's say I have

  • Fries Price 150
  • Burger Price 200
  • Salad Price 100
  • Bacon Price 200

So I have a total of 650 money

The output works, but if I have placed these data into a model class, which follows the MVC pattern. It does not add up or increment the values.

1 Answers

So it looks like that checkEvent function was for a single checkbox and then grew to handle other checkboxes as well. So do you see how in your check event function it is alot of the same code being repeated

if (cb1.isSelected()) {
    price = 150;
    message += cb1.getText() + " Price: " + price  + "\n";
}

This for example if you can pull out what is changing and pass it as a parameter then you only need one or two if statements not 4 or any number of checkboxes you pass it. private void checkEvent(CheckBox checkBox, int price) I pulled out the price because it has the ability to be different and I pass the checkbox so that it can reach the set text for itself then you only have to write the code once

private void checkEvent(CheckBox checkBox, int price){
    String message = checkBox.getText() + " Price: " + price  + "\n";
    if(checkBox.isSelected()){
        totalCost = totalCost + price;
        messageListLabel.setText(messageListLabel.getText() + message);
    } else {
        totalCost = totalCost - price;
        messageListLabel.setText(messageListLabel.getText().replace(message, ""));
    }
    totalCostLabel.setText("\nTotal Cost:"+totalCost);
}

Below is a full runnable example

public class Main extends Application {

    private int totalCost = 0;
    private Label messageListLabel;
    private Label totalCostLabel;


    @Override
    public void start(Stage primaryStage) {
        CheckBox checkBoxOne = new CheckBox("Fries");
        checkBoxOne.setOnAction(event -> checkEvent(checkBoxOne, 150));

        CheckBox checkBoxTwo = new CheckBox("Burger");
        checkBoxTwo.setOnAction(event -> checkEvent(checkBoxTwo, 200));

        CheckBox checkBoxThree = new CheckBox("Salmon");
        checkBoxThree.setOnAction(event -> checkEvent(checkBoxThree, 100));

        CheckBox checkBoxFour = new CheckBox("Bacon");
        checkBoxFour.setOnAction(event -> checkEvent(checkBoxFour, 200));

        messageListLabel = new Label();
        totalCostLabel = new Label();

        VBox vbox = new VBox(
                new Label("Pic what ya want!!!"),
                checkBoxOne,
                checkBoxTwo,
                checkBoxThree,
                checkBoxFour,
                messageListLabel,
                totalCostLabel
        );
        vbox.setAlignment(Pos.CENTER);

        primaryStage.setScene(new Scene(vbox, 150, 250));
        primaryStage.show();
    }

    private void checkEvent(CheckBox checkBox, int price){
        String message = checkBox.getText() + " Price: " + price  + "\n";
        if(checkBox.isSelected()){
            totalCost = totalCost + price;
            messageListLabel.setText(messageListLabel.getText() + message);
        } else {
            totalCost = totalCost - price;
            messageListLabel.setText(messageListLabel.getText().replace(message, ""));
        }
        totalCostLabel.setText("\nTotal Cost:"+totalCost);
    }
}
Related