Search code examples
javaswingjtextarea

updates a jtextarea every key input


I want to have 2 JTextAreas. the first one is where the user types in and the second one is where the binary equivalent of the input will appear. Is it possible and how to make the second textarea updates everytime the user inputs a character??? btw, the second textarea will be not editable by the user.


Solution

  • Add a change listener on the document of the first the first text area.

    jTextArea1.getDocument().addDocumentListener(new DocumentListener() {
        @Override
        public void changedUpdate(DocumentEvent evt) {
            dumpBinary(evt, jTextArea2);
        }
        @Override
        public void insertUpdate(DocumentEvent evt) {
            dumpBinary(evt, jTextArea2);
        }
        @Override
        public void removeUpdate(DocumentEvent evt) {
            dumpBinary(evt, jTextArea2);
        }
    });