简体   繁体   中英

How to compare 2 Field Text with each other in Javafx

I have 2 Field Text in a BankAccount class, 1 for deposit, 1 to withdraw.

在此处输入图片说明

Both of the field text take only double values:

TextField depositInput = new TextField();
TextField withdrawInput = new TextField();

And 1 button to execute these field texts (can be both).

Button button = new Button("Execute");

The problem is I don't know how to tell Java which TextField has been executed. Since I want to display different messages when the user wants to either deposit or withdraw:

For example:

if(FieldText executed is deposit){
    println("User deposits X amount");
    double depositAmount = X;

}

if(FieldText executed is withdraw){
    println("User withdraws X amount");
    double withdrawAmount = -X;
}

I have tried:

if(FieldText == depositInput){
}

but Java shows me an error.

If I got your problem right, the simplest solution would be just to check if the value of the text field is not empty upon button press:

TextField depositInput = new TextField();
TextField withdrawInput = new TextField();
Button button = new Button("Execute");
button.setOnAction((event) -> {
    if (!depositInput.getText().isEmpty() && !withdrawInput.getText().isEmpty()) {
        System.out.println("Deposit&Withdrawal");
    } else if (!depositInput.getText().isEmpty()) {
        System.out.println("Deposit");
    } else if (!withdrawInput.getText().isEmpty()) {
        System.out.println("Withdrawal");
    }
});

If you are working JavaFX with scene builder

@FXML
    private PasswordField PASSWORD;

    @FXML
    private PasswordField REPASSWORD; 

void validateFields(ActionEvent event) {

if (PASSWORD.getText() == null ? REPASSWORD.getText() != null : !PASSWORD.getText().equals(REPASSWORD.getText()) ){

       Alert alert = new Alert(Alert.AlertType.WARNING);
            alert.setTitle("Warning");
            alert.setHeaderText("PASSWORD ERROR");
            alert.setContentText("CHECK YOUR PASSWORDS . "
                    + "\nPlease try again.");
            alert.showAndWait(); 
       }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM