2
JTextArea area1 = new JTextArea();
JTextArea area2 = new JTextArea();
DocumentListener documentListener = new DocumentListener() {
      public void changedUpdate(DocumentEvent documentEvent) {
        printIt(documentEvent);
      }
      public void insertUpdate(DocumentEvent documentEvent) {
        printIt(documentEvent);
      }
      public void removeUpdate(DocumentEvent documentEvent) {
        printIt(documentEvent);
      }
      private void printIt(DocumentEvent documentEvent) {
        DocumentEvent.EventType type = documentEvent.getType();
        String typeString = null;
        if (type.equals(DocumentEvent.EventType.CHANGE)) {
          typeString = "(CHANGED KEY) ";
        }  else if (type.equals(DocumentEvent.EventType.INSERT)) {
          typeString = "(PRESSED KEY) ";
        }  else if (type.equals(DocumentEvent.EventType.REMOVE)) {
          typeString = "(DELETED KEY) ";
        }
        System.out.print("Type : " + typeString);
        Document source = documentEvent.getDocument();
        int length = source.getLength();
        System.out.println("Current size: " + length);

      }
    };
area1.getDocument().addDocumentListener(documentListener);
area2.getDocument().addDocumentListener(documentListener);

This is my code for handling when things are pressed in either area1 or area2.

I am trying to make it so that when one area's text is updated, it updates the second area's text with the same text and vice versa. How would I go about doing so? One field is for encrypting something and the other for the decrypted values and vice versa.

Huang Lee
  • 53
  • 4
  • 1
    Be careful with modifying text components from with a `DocumentListener`, the API has guards in place to prevent this from occurring and will throw exceptions... – MadProgrammer Oct 16 '14 at 00:01

1 Answers1

2

Just have them share the same Document, that's it.

area1.setDocument(area2.getDocument());
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373