It is strange that there is no easy way to get this information.
I looked at the source code of the Swing libraries. Of course, this information is contained in the DocumentEvent , which belongs to the AbstractDocument$DefaultDocumentEvent , which contains the protected Vector<UndoableEdit> edits , which contains one element of the GapContent$RemoveUndo , which contains the protected String string , which is used only in this class (no other classes "package" get this), and this RemoveUndo class RemoveUndo not have a getter for this field.
Even toString did not show it (because RemoveUndo did not override the toString method):
[ javax.swing.text.GapContent$RemoveUndo@6303ddfd hasBeenDone: true alive: true]
It is so strange for me that I believe that there is another easy way to get the deleted row and that I just donβt know how to execute it.
One thing you can do is most obvious:
final JTextArea textArea = new JTextArea(); textArea.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { previousText = textArea.getText(); } }); textArea.getDocument().addDocumentListener(new DocumentListener() { @Override public void removeUpdate(DocumentEvent e) { if(previousText != null) { String removedStr = previousText.substring(e.getOffset(), e.getOffset() + e.getLength()); System.out.println(removedStr); } } @Override public void insertUpdate(DocumentEvent e) { } @Override public void changedUpdate(DocumentEvent e) { } });
where previousText is the instance variable.
or (most nasty):
textArea.getDocument().addDocumentListener(new DocumentListener() { @Override public void removeUpdate(DocumentEvent e) { String removedString = getRemovedString(e); System.out.println(removedString); } @Override public void insertUpdate(DocumentEvent e) { } @Override public void changedUpdate(DocumentEvent e) { } });
plus this method:
public static String getRemovedString(DocumentEvent e) { try { Field editsField = null; Field[] fields = CompoundEdit.class.getDeclaredFields(); for(Field f : fields) { if(f.getName().equals("edits")) { editsField = f; break; } } editsField.setAccessible(true); List edits = (List) editsField.get(e); if(edits.size() != 1) { return null; } Class<?> removeUndo = null; for(Class<?> c : GapContent.class.getDeclaredClasses()) { if(c.getSimpleName().equals("RemoveUndo")) { removeUndo = c; break; } } Object removeUndoInstance = edits.get(0); fields = removeUndo.getDeclaredFields(); Field stringField = null; for(Field f : fields) { if(f.getName().equals("string")) { stringField = f; break; } } stringField.setAccessible(true); return (String) stringField.get(removeUndoInstance); } catch(SecurityException e1) { e1.printStackTrace(); } catch(IllegalArgumentException e1) { e1.printStackTrace(); } catch(IllegalAccessException e1) { e1.printStackTrace(); } return null; }
source share