java - Find Removed text in JTextPane -
i learning documentlistener in java jtextpane. i'm trying current string or
word (text) inserted/removed in textpane. i've tried this:
import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import javax.swing.text.*; class changeeffect implements documentlistener { @override public void insertupdate(documentevent de) { int offset = de.getoffset(); try { string str = de.getdocument().gettext(offset,de.getlength()); system.out.println("you entered text: " + str); }catch(exception e){e.printstacktrace();} } @override public void changedupdate(documentevent de) { } @override public void removeupdate(documentevent de) { try { string s = de.getdocument().gettext(de.getoffset(),de.getlength()); system.out.println("removed text is: " +s); }catch(exception e){} } } public class multimodecolor { private static void displaygui() { jframe frame = new jframe("demo"); jtextpane text = new jtextpane(); frame.add(text); frame.pack(); frame.setdefaultcloseoperation(jframe.dispose_on_close); frame.setvisible(true); text.getdocument().adddocumentlistener(new changeeffect()); } public static void main(string[] arg) { runnable r = new runnable() { @override public void run() { displaygui(); } }; swingutilities.invokelater(r); } }
in above example got inserted text removed text display nothing(except message in println statement).
is there way removed text document?
try using documentfilter
instead of documentlistener
. main problem you're facing documentlistener
being notified after fact, meaning time you're listener notified, text has being removed.
the documentfilter
on other gets notified first...
import java.awt.borderlayout; import java.awt.eventqueue; import javax.swing.jframe; import javax.swing.jscrollpane; import javax.swing.jtextarea; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import javax.swing.text.abstractdocument; import javax.swing.text.attributeset; import javax.swing.text.badlocationexception; import javax.swing.text.documentfilter; public class documentmonitor { public static void main(string[] args) { new documentmonitor(); } public documentmonitor() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { } jtextarea ta = new jtextarea(20, 20); ((abstractdocument) ta.getdocument()).setdocumentfilter(new documentfilter() { @override public void replace(documentfilter.filterbypass fb, int offset, int length, string text, attributeset attrs) throws badlocationexception { if (offset >= fb.getdocument().getlength()) { system.out.println("added: " + text); } else { string old = fb.getdocument().gettext(offset, length); system.out.println("replaced " + old + " " + text); } super.replace(fb, offset, length, text, attrs); } @override public void insertstring(documentfilter.filterbypass fb, int offset, string text, attributeset attr) throws badlocationexception { system.out.println("added: " + text); super.insertstring(fb, offset, text, attr); } @override public void remove(documentfilter.filterbypass fb, int offset, int length) throws badlocationexception { system.out.println("removed: " + fb.getdocument().gettext(offset, length)); super.remove(fb, offset, length); } }); jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new borderlayout()); frame.add(new jscrollpane(ta)); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } }
yes, using jtextarea
, process same jtextpane
...
Comments
Post a Comment