/** * Copies the text passed in from the component to the model, replacing any content already there * entirely. */ public void updateModelFromComponent(String newText) { try { doc.remove(0, doc.getLength()); doc.insertString(0, newText, null); componentJustChanged = true; // Let the twin routine know not to run // as a result of this change (no need to // sync component/model twice). } catch (BadLocationException e) { // Shouldn't ever happen since we're replacing the whole lot e.printStackTrace(); } }
/** Reads the text from the model and replaces the text in the component. */ public void updateComponentFromModel() { // We set this flag when a keypress fires a model // change - this ensures that if the model was just // updated from the component, the resulting Document // change doesn't fire a component update (losing // the caret position and looking weird to the user). // Besides - if this flag is set, the two have just // been synced so there's no need to do it again. if (componentJustChanged) { componentJustChanged = false; return; } try { comp.setText(doc.getText(0, doc.getLength())); } catch (BadLocationException e) { // Shouldn't ever happen e.printStackTrace(); } }