繁体   English   中英

相互更新的javafx文本字段

[英]javafx Text Fields that update each other

我正在编写一个基本的GUI应用程序,其中涉及一个TextFields窗格,表示将字节输入程序缓冲区的5种方法。 这个想法是,当您编辑其中一个文本字段时,其他4个字段将使用您正在编辑的文本字段的翻译后的值进行更新。 例如,将十六进制字段更改为“ FF”将使十进制字段立即更改为“ 255”,依此类推。

问题在于,我无法弄清楚如何避免在更改一个更改时出现无限循环:更改十六进制字段,修改十进制字段,然后尝试再次更新该十六进制字段,依此类推,创建一个堆栈溢出错误。

以下是定义字段及其侦听器的代码:

    TextField Inputchar = new TextField();
    Inputchar.setPromptText("Enter " + Program.BYTES_OF_BUFFER + " character(s)...");
    Inputchar.setMinWidth(250);

    TextField Inputhex = new TextField();
    Inputhex.setPromptText("Enter a value between 0 and " + Program.BUFFER_SIZE.toString(16) + "...");

    TextField Inputdec = new TextField();
    Inputdec.setPromptText("Enter a value between 0 and " + Program.BUFFER_SIZE.toString(10) + "...");

    TextField Inputoct = new TextField();
    Inputoct.setPromptText("Enter a value between 0 and " + Program.BUFFER_SIZE.toString(8) + "...");

    TextField Inputbin = new TextField();
    Inputbin.setPromptText("Enter a value between 0 and " + Program.BUFFER_SIZE.toString(2) + "...");

    Inputchar.textProperty().addListener(c -> {
        char[] cs = Inputchar.getText().toCharArray();
        byte[] b = new byte[cs.length];
        for (int i = 0; i < cs.length; i++) {
            b[i] = (byte) cs[i];
        }
        BigInteger B = new BigInteger(b);
        Inputhex.setText(B.toString(16));
        Inputdec.setText(B.toString(10));
        Inputoct.setText(B.toString(8));
        Inputbin.setText(B.toString(2));
    });

    Inputhex.textProperty().addListener(c -> {
        BigInteger B = BigInteger.valueOf(Integer.parseInt(Inputhex.getText(), 16));
        Inputchar.setText(BF_Program.bytestochars(B.toByteArray()));
        Inputdec.setText(B.toString(10));
        Inputoct.setText(B.toString(8));
        Inputbin.setText(B.toString(2));
    });

    Inputdec.textProperty().addListener(c -> {
        BigInteger B = BigInteger.valueOf(Integer.parseInt(Inputdec.getText(), 10));
        Inputchar.setText(BF_Program.bytestochars(B.toByteArray()));
        Inputhex.setText(B.toString(16));
        Inputoct.setText(B.toString(8));
        Inputbin.setText(B.toString(2));
    });

    Inputoct.textProperty().addListener(c -> {
        BigInteger B = BigInteger.valueOf(Integer.parseInt(Inputoct.getText(), 8));
        Inputchar.setText(BF_Program.bytestochars(B.toByteArray()));
        Inputhex.setText(B.toString(16));
        Inputdec.setText(B.toString(10));
        Inputbin.setText(B.toString(2));
    });

    Inputbin.textProperty().addListener(c -> {
        BigInteger B = BigInteger.valueOf(Integer.parseInt(Inputbin.getText(), 2));
        Inputchar.setText(BF_Program.bytestochars(B.toByteArray()));
        Inputhex.setText(B.toString(16));
        Inputdec.setText(B.toString(10));
        Inputoct.setText(B.toString(8));
    });

如何更改此设置,以便编辑一个字段将更新其他字段一次而不循环? 谢谢。

原来我让我的lamba表达式实现错误类的方式:InvalidationListener,应该使用ChangeListener时:

    Inputchar.textProperty().addListener((observable, oldValue, newValue) -> {
        char[] cs = Inputchar.getText().toCharArray();
        byte[] b = new byte[cs.length];
        for (int i = 0; i < cs.length; i++) {
            b[i] = (byte) cs[i];
        }
        BigInteger B = new BigInteger(b);
        Inputhex.setText(B.toString(16));
        Inputdec.setText(B.toString(10));
        Inputoct.setText(B.toString(8));
        Inputbin.setText(B.toString(2));
    });

    Inputhex.textProperty().addListener((observable, oldValue, newValue) -> {
        BigInteger B = BigInteger.valueOf(Integer.parseInt(Inputhex.getText(), 16));
        Inputchar.setText(BF_Program.bytestochars(B.toByteArray()));
        Inputdec.setText(B.toString(10));
        Inputoct.setText(B.toString(8));
        Inputbin.setText(B.toString(2));
    });

    Inputdec.textProperty().addListener((observable, oldValue, newValue) -> {
        BigInteger B = BigInteger.valueOf(Integer.parseInt(Inputdec.getText(), 10));
        Inputchar.setText(BF_Program.bytestochars(B.toByteArray()));
        Inputhex.setText(B.toString(16));
        Inputoct.setText(B.toString(8));
        Inputbin.setText(B.toString(2));
    });

    Inputoct.textProperty().addListener((observable, oldValue, newValue) -> {
        BigInteger B = BigInteger.valueOf(Integer.parseInt(Inputoct.getText(), 8));
        Inputchar.setText(BF_Program.bytestochars(B.toByteArray()));
        Inputhex.setText(B.toString(16));
        Inputdec.setText(B.toString(10));
        Inputbin.setText(B.toString(2));
    });

    Inputbin.textProperty().addListener((observable, oldValue, newValue) -> {
        BigInteger B = BigInteger.valueOf(Integer.parseInt(Inputbin.getText(), 2));
        Inputchar.setText(BF_Program.bytestochars(B.toByteArray()));
        Inputhex.setText(B.toString(16));
        Inputdec.setText(B.toString(10));
        Inputoct.setText(B.toString(8));
    });

这实际上有效。 跟上来以备将来参考。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM