Java FX根据另一个TextField值更新一个TextField

时间:2019-03-15 08:58:57

标签: java javafx

我是JAVA FX的新手,我想根据另一个TextField值“实时”更新一个TextField。

这是我的代码段:

/resources
    |   electron.asar
    |   
    \---app
        |   
        +---.cache
        |   |   compiler-info.json.gz
        |           
        +---node_modules
        |           
        \---src
            |   index.html
            |   index.js
            |   
            +---css
            |       index.css
            |       
            +---images
            |       svg-logo.svg
            |       
            +---js
            |       jquery-3.3.1.min.js
            |       main.js
            |       
            \---svg

我应该在第二个TextField上添加另一个列表器吗?

2 个答案:

答案 0 :(得分:0)

为我工作。请注意,以下代码不需要.fxml文件。您发布的代码中对方法substring()的调用是否抛出了您不知道的Exception,因为您将其捕获在空的catch块中?当然,我只是在猜测,因为您只发布了部分代码。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class JfxTst00 extends Application {
    public void start(Stage mainStage) throws Exception {
        mainStage.setTitle("JfxTst00");
        BorderPane root = new BorderPane();
        TextField tf_NewCode = new TextField();
        TextField tf_Code = new TextField();
        tf_Code.textProperty().addListener((observable, oldVal, newVal) -> tf_NewCode.setText(newVal));
        root.setTop(tf_Code);
        root.setBottom(tf_NewCode);
        Scene scene = new Scene(root, 220, 70);
        mainStage.setScene(scene);
        mainStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

答案 1 :(得分:0)

您的问题实际上并不能解释您面临的问题,尽管我看到您应该遇到的一些问题。

首先,对于第一个TextField,您只需要一个侦听器,因为这是我们正在观察变化的侦听器。

然后,您需要考虑输入到TextField中的少于2个字符且超过6个字符的情况。由于您在subString(2, 6)呼叫中设置了硬限制,因此我们只希望我们的监听器起作用在这些限制之内。

这是一个演示以下内容的简单文本应用程序:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TextFieldBinding extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        // Simple interface
        VBox root = new VBox(5);
        root.setPadding(new Insets(10));
        root.setAlignment(Pos.CENTER);

        TextField txt1 = new TextField();
        TextField txt2 = new TextField();

        txt1.textProperty().addListener((observable, oldValue, newValue) -> {

            // First, only update txt2 if the new value entered in txt1 is greater than 2, otherwise
            // our substring() method will throw an exception
            if (newValue.length() > 2) {

                // We also need to prevent trying to get a substring that exceeds the remaining length
                // of the txt1 input
                int maxIndex;
                if (newValue.length() < 6) {
                    maxIndex = newValue.length();
                } else {
                    maxIndex = 6;
                }

                // Now set the text for txt2
                txt2.setText(newValue.substring(2, maxIndex));
            }
        });

        root.getChildren().addAll(txt1, txt2);

        // Show the Stage
        primaryStage.setWidth(300);
        primaryStage.setHeight(300);
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
}
相关问题