JavaFx:如何使用聚焦属性?

时间:2017-09-24 10:03:06

标签: javafx focus

我有两个TextField QntMatr指的是物质数量,UniteMatr指的是数量单位。当用户将光标放在QntMatrUniteMatr时,我需要按钮addMatrButton应该被禁用,并且当QntMatr和UnitrMatr都不为空时它将被启用。我尝试在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);

                    }

                }

            }
        });

1 个答案:

答案 0 :(得分:2)

如果要根据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();
    }

}