JavaFx:How 要使用重点 属性?

JavaFx:How to use focused property?

我有两个 TextField QntMatr 是指物质的数量,UniteMatr 是指当用户将光标放在 QntMatr 或 [=14 上时需要 quantity.I 的单位=],按钮 addMatrButton 应该被禁用,并且当 QntMatr 和 UnitrMatr 不为空时它将被启用 them.I 尝试在 UniteMatrQntMatr 之间绑定但是我做到了不知道具体方法。

代码

 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);

                    }

                }

            }
        });

如果您想根据 TextField 的内容禁用按钮(如果为空则为 true,否则为 false),那么您应该将 Button 的 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();
    }

}