While I was looking for a way to check if a JEditorPane's text had been changed since the last save, I found a site that actually wanted me to pay for the answer. Bloody cheek. It isn't that tough, but it took a bit of reading. All that is needed is to install a DocumentListener into the editor pane's document. You can download a complete example which includes an ant build file. Just type "ant run" at the shell prompt to run it. You will need ant and JDK >= 1.5 (it may work in older JDKs, but I used 1.5 so I can't be sure).
- private boolean dirty = false;
-
- public SwingEditor() {
- super("text/plain", "");
- getDocument().addDocumentListener(
- new EditDocumentListener());
- }
-
- public boolean isDirty() {
- return dirty;
- }
-
- public void setDirty(boolean newDirty) {
- dirty = newDirty;
- }
-
- private class EditDocumentListener implements
-
- dirty = true;
- }
-
- dirty = true;
- }
-
- dirty = true;
- }
- }
- }
Source download: swing-editor-src-0_1.jar