How do I know if any changes have been made to jtextarea or not?

I created jtextarea where the user can modify its contents. I want to know if there is any way whether the user changed his content or not before closing the application. Please, help.
-Thanks in advance

+4
source share
2 answers

You need to add a DocumentListener to a Document that supports the text area.

Then, in the callback methods (insertUpdate (), removeUpdate (), changedUpdate ()) of the listener, simply set the flag that changed something, and check this flag before closing the application

  public class MyPanel
   implements DocumentListener
 {
   private boolean changed;

   public MyPanel ()
   {
     JTextArea textArea = new JTextArea ();
     textArea.getDocument (). addDocumentListener (this);
     .....
   }

   .....

   public void insertUpdate (DocumentEvent e)
   {
     changed = true;
   }
   public void removeUpdate (DocumentEvent e)
   {
     changed = true;
   }
   public void changedUpdate (DocumentEvent e)
   {
     changed = true;
   }
 }
+8
source

Save the jtextarea value and compare this value with the jtextarea value when you close the application.

Pseudocode here does not remember the syntax of the text area syntax:

String oldText = textarea.getText(); .... // not the exact method, just to point the moment of application exit public onClose() { String newText = textArea.getText(); // assuming oldText is not null if (oldText.equals(newText)) { // no changes have been done } else { // the value changed } } 
+1
source

Source: https://habr.com/ru/post/1337581/


All Articles