除非在javaFX中有两个事件,否则如何将按钮设置为禁用?

时间:2022-06-27 17:03:20

I have two separate textProperty listeners to listen to changes on my javaFX application so it can update a password strength icon, and a valid username icon respectively. Both functions take their data from a TextField (username) and a PasswordField (password). These are the listeners:

我有两个单独的textProperty侦听器来监听我的javaFX应用程序上的更改,因此它可以分别更新密码强度图标和有效的用户名图标。这两个函数都从TextField(用户名)和PasswordField(密码)中获取数据。这些是听众:

if(loginCheck.passwordValidator(password.getText()) == -1) {
    passwordImg.setImage(fail);
    submit.setDisable(true);
}

if(loginCheck.usernameValidator(username.getText()) == -1) {
    usernameImg.setImage(fail);
    submit.setDisable(true);
}

I'm trying to get the button (submit) to stay disabled when one or the other functions return -1. I can disable the button when either function has the correct output, but I can't enable the button again without using separate submit.setDisable(false) calls; which falls flat on its face when one listener is trying to enable the button, when it should still be disabled according to the other listener. Is there a way I can set the button to ALWAYS stay off unless both events are true?

当一个或另一个函数返回-1时,我试图让按钮(提交)保持禁用状态。我可以在任一函数输出正确时禁用该按钮,但是如果不使用单独的submit.setDisable(false)调用,我就无法再次启用该按钮;当一个听众试图启用该按钮时,它仍然会在另一个听众仍然被禁用时掉在脸上。有没有办法我可以将按钮设置为始终保持关闭,除非两个事件都是真的?

Thanks in advance!

提前致谢!

1 个解决方案

#1


2  

Just combine your conditions using logic OR operation -- ||

只需使用逻辑或运算 - ||组合您的条件

boolean invalidPassword = loginCheck.passwordValidator(password.getText()) == -1;
boolean invalidUsername = loginCheck.usernameValidator(username.getText()) == -1;

if (invalidPassword) {
    passwordImg.setImage(fail);
}

if(invalidUsername) {
    usernameImg.setImage(fail);
}

// if at least one of variables is true then submit will be disabled
submit.setDisable(invalidPassword || invalidUsername);

#1


2  

Just combine your conditions using logic OR operation -- ||

只需使用逻辑或运算 - ||组合您的条件

boolean invalidPassword = loginCheck.passwordValidator(password.getText()) == -1;
boolean invalidUsername = loginCheck.usernameValidator(username.getText()) == -1;

if (invalidPassword) {
    passwordImg.setImage(fail);
}

if(invalidUsername) {
    usernameImg.setImage(fail);
}

// if at least one of variables is true then submit will be disabled
submit.setDisable(invalidPassword || invalidUsername);