public PlainView(Document doc, JTextComponent component) { super(doc); this.comp = component; // Subscribe to key presses so we know when to mark the document // as updated comp.addKeyListener( new KeyAdapter() { public void KeyTyped(KeyEvent e) { updateModelFromComponent(comp.getText()); } }); // Subscribe to Document events so we know when to update // the component from the model. doc.addDocumentListener( new DocumentListener() { public void insertUpdate(DocumentEvent e) { updateComponentFromModel(); } public void removeUpdate(DocumentEvent e) { updateComponentFromModel(); } public void changedUpdate(DocumentEvent e) { updateComponentFromModel(); } }); }