0

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

enter image description here

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.

c0der
  • 18,467
  • 6
  • 33
  • 65
Tamamo
  • 73
  • 1
  • 1
  • 8
  • 1
    I think you want this [Value Change Listener for JavaFX's TextField](https://stackoverflow.com/questions/30160899/value-change-listener-for-javafxs-textfield) – c0der Jul 21 '18 at 12:43
  • @c0der actually I'm not looking for whether the user changes the value inside the TextField, I want to know which TextField the user is currently using. – Tamamo Jul 21 '18 at 13:03
  • define "currently using". Has focused ? Being edited ? Has changed ? "has been executed" does not mean much to me. – c0der Jul 21 '18 at 13:16
  • @c0der basically the format is like this: i.imgur.com/m1EckSc.png. Say I put 100 in the left box (which is deposit), then press the button, that means to execute it -> how do I let java know which Field Text I enter 100, the left one or the right on? – Tamamo Jul 21 '18 at 13:40
  • 1
    process both `TextField`s. and check if values are valid. If a value is valid make a deposit or withdrawal transaction. Clear `TextField` after successful processing. – c0der Jul 21 '18 at 14:58

2 Answers2

2

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");
    }
});
Enigo
  • 3,685
  • 5
  • 29
  • 54
1

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(); 
       }
}
Jodast
  • 1,279
  • 2
  • 18
  • 33