Articles Projects Tips Downloads Contacts About

Refresh Views to show document's changes in JEditorPane.

Custom operations with Document structure or Document's elements attributes need to be reflected in Views. For such cases the following refresh() method was written. The refresh method generates a dummy event to notify document listeners about structure changes in given position and offset. The fireChangedUpdate() method of DefaultStyledDocument is protected so the refresh() method should be added in a class extending DefaultStyledDocument.

    public class RefreshDocument extends DefaultStyledDocument {
 
        public void refresh() {
            refresh(0, getLength());
        }
        
        public void refresh(int offset, int len) {
            DefaultDocumentEvent changes = new DefaultDocumentEvent(offset, len, 
                DocumentEvent.EventType.CHANGE);
            Element root = getDefaultRootElement();
            Element[] removed = new Element[0];
            Element[] added = new Element[0];
            changes.addEdit(new ElementEdit(root, 0, removed, added));
            changes.end();
            fireChangedUpdate(changes);
        }
    }