简体   繁体   English

javaFX文本字段和侦听器

[英]javaFX text fields and listeners

I am facing a situation where there are two text fields with 2 seperate listeners for each of them. 我面临的情况是,两个文本字段分别具有两个独立的侦听器。

TextField customerId and TextField customerName. TextField customerId和TextField customerName。

1000 mohan 1000莫汉

1002 mithun 1002米通

I am trying to automatically update other text field when one text field is filled eg if customerId 1000 is filled then corresponding customer name mohan get updated to text field customerName ,and if mohan is filled then his customer id 1000 will get filled in customerId text field.I am using maps,The problem is when one text field filled its listener is called which call backs the same text field listener and this causes cycles finally ending up with lot off errors.What should i do to solve this ? 我试图在填充一个文本字段时自动更新其他文本字段,例如,如果填充了customerId 1000,则将相应的客户名mohan更新为文本字段customerName;如果填充了mohan,则其客户ID 1000,将被填充到customerId文本字段中我正在使用地图,问题是当一个文本字段填充其侦听器时被调用,它调用了相同的文本字段侦听器,这导致循环最终以很多错误结束。我该怎么办?

Minimal example 最小的例子

    Map<String, String> treeMapCustomerName,treeMapCustomerId;

     treeMapCustomerName=new TreeMap<String,String>();
     treeMapCustomerId=new TreeMap<String,String>();
String customerName="mohan";
String customerId="1000";

treeMapCustomerId.put("1000","Mohan");
treeMapCustomerId.put("1002","Mithun");

treeMapCustomerName.put("Mohan","1000");
treeMapCustomerName.put("Mithun","1002");




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

        customerId.setText(treeMapCustomerName.get(customerName));//immediately customerId textfield listener is triggered which will trigger this listener causing cycles  

        });

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

        customerName.setText(treeMapCustomerId.get(customerId));

        });

You aren't taking advantage of your new values, instead you are accessing the map with the control, which will throw errors at run time 您没有利用新值,而是使用控件访问地图,该控件将在运行时引发错误

You could check if the map contains your key and only update the other text field if it is present, something like the below: 您可以检查地图是否包含您的密钥,并且仅更新另一个文本字段(如果存在),如下所示:

            customerName.textProperty().addListener((observable, oldValue, newValue) -> {
                if(treeMapCustomerName.containsKey(newValue)){
                    customerId.setText(treeMapCustomerName.get(newValue));
                }
            });

            customerId.textProperty().addListener((observable, oldValue, newValue) -> {
                if(treeMapCustomerId.containsKey(newValue)){
                    customerName.setText(treeMapCustomerId.get(newValue));
                }
            });

This will avoid issues with checking the map before a complete id / username has been entered, however this won't account for issues where a value being entered is a sub string of another. 这样可以避免在输入完整的ID /用户名之前检查地图的问题,但这不会解决输入的值是另一个子字符串的问题。

Eg If the map contained id's 100, 1000, 10000 and you don't want each of these to display as the user types 10000, you may need an additional control such as a button instead of using the property 例如,如果地图包含id的100、1000、10000,并且您不希望在用户键入10000时显示其中的每一个,则可能需要其他控件(例如按钮)而不是使用属性

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

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