vendredi 18 juin 2021

JavaFX if statement in ChoiceBox

I am trying to make a simple calculator using JavaFX. Here is my code:

package sample;

import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;
//import java.awt.*;
import javafx.event.ActionEvent;
import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable {

    @FXML
    private ChoiceBox myChoiceBox;
    @FXML
    private TextField firstTxt, secondTxt, result;
    @FXML
    private String plus = "+";
    private String minus = "-";
    @Override
    public void initialize(URL arg0, ResourceBundle arg1){

        myChoiceBox.getItems().addAll(plus,minus);
    }


    @FXML
    public void addition(ActionEvent event){
        if(myChoiceBox.equals(plus)) {
            double first = Double.parseDouble(firstTxt.getText());
            double second = Double.parseDouble(secondTxt.getText());
            double result1 = first + second;
            result.setText(String.valueOf(result1));
        }
    }



}

So in the addition class the if statement not working! I also tried something like that: if(myChoiceBox.equals("+")) nothing change. Any ideas? Thank you!

Aucun commentaire:

Enregistrer un commentaire