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);
}
You can use a boolean to mark when you are propagating changes.