Question: I have a JPanel with two JTextFields. If the user writes some text into textfield A the same text should appear in textfield B and vice versa.

How can that be implemented without getting an infinite loop.

So far I have the following which leds to a infinite loop.

```
        JTextField textFieldA;
        JTextField textFieldB;

        textFieldA.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent e) {  
                Document doc = (Document)e.getDocument();
                String line = doc.getText(0, doc.getLength());  
                textFieldB.setText(line);
            }
            textFieldB.getDocument().addDocumentListener(new DocumentListener() {
                public void insertUpdate(DocumentEvent e) {  
                    Document doc = (Document)e.getDocument();
                    String line = doc.getText(0, doc.getLength());  
                    textFieldA.setText(line);
                }

```

Answer: 
Take advantage of MVC design and share the model:

```
textFieldB.setDocument(textFieldA.getDocument());

```
I'd do it with a key listener on both Text fields, the keyPressed event then copys the triggered field to the other. greets