JavaFx:如何使用聚焦属性?
我有两个文本字段JavaFx:如何使用聚焦属性?,javafx,focus,Javafx,Focus,我有两个文本字段QntMatr表示物质的数量,UniteMatr表示数量的单位。我需要当用户将光标放在QntMatr或UniteMatr上时,按钮addMatrButton应该被禁用,当QntMatr和UnitrMatr都不为空时,它将启用。我尝试了UniteMatr和QntMatr之间的绑定,但我不知道确切的方法 代码 QntMatr.focusedProperty().addListener(new ChangeListener<Boolean>() {
QntMatr
表示物质的数量,UniteMatr
表示数量的单位。我需要当用户将光标放在QntMatr
或UniteMatr
上时,按钮addMatrButton
应该被禁用,当QntMatr和UnitrMatr都不为空时,它将启用。我尝试了UniteMatr
和QntMatr
之间的绑定,但我不知道确切的方法
代码
QntMatr.focusedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if (newValue) {
if (QntMatr.getText().isEmpty() && UniteMatr.getText().isEmpty()) {
AddMatrButton.setDisable(true);
} else {
AddMatrButton.setDisable(false);
}
}
}
});
UniteMatr.focusedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if (newValue) {
if (QntMatr.getText().isEmpty() && UniteMatr.getText().isEmpty()) {
AddMatrButton.setDisable(true);
} else {
AddMatrButton.setDisable(false);
}
}
}
});
QntMatr.focusedProperty().addListener(新的ChangeListener()){
@凌驾
public void已更改(observeValue如果您希望根据TextField内容禁用按钮(如果为空,则为true,否则为false),则应将按钮的disableProperty与TextField的textProperty绑定。以下是一个示例:
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class TestClass extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
HBox box = new HBox(10);
box.setPadding(new Insets(10));
TextField field = new TextField();
Button button = new Button("OK");
button.disableProperty().bind(Bindings.isEmpty(field.textProperty()));
box.getChildren().addAll(field, button);
stage.setScene(new Scene(box));
stage.show();
}
}
如果您想制作更复杂的绑定示例,以检查字段是否为非焦点,您可以执行以下操作:
import javafx.application.Application;
import javafx.beans.binding.BooleanBinding;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class TestClass extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
HBox box = new HBox(10);
box.setPadding(new Insets(10));
TextField field = new TextField();
Button button = new Button("OK");
button.disableProperty().bind(new BooleanBinding() {
{
bind(field.textProperty());
bind(field.focusedProperty());
}
@Override
protected boolean computeValue() {
// you can check if the field is focused
// of if it's content is empty etc.
return field.getText().isEmpty();
}
});
box.getChildren().addAll(field, button);
stage.setScene(new Scene(box));
stage.show();
}
}
如果TextField
s中的一个被聚焦,但是TextField
s都是非空的,会发生什么情况?在我看来,用户需要聚焦一些其他控件以启用按钮,这似乎是不好的用户体验…不,QntMatr
和UnitMatr
是可选字段。用户无法添加一个数量,并将单位留空。