I've created a jtextarea where a user can modify its content. I want to know,if there is any way, whether the user has modified its content or not before closing the application. Please help.
-Thanks in advance
You need to add a DocumentListener to the Document that backs the text area.
Then in the callback methods (insertUpdate(), removeUpdate(), changedUpdate()) of the listener, simply set a flag that something has changed and test that 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; } }
Save the value of jtextarea and compare this value to the value of jtextarea in the moment of application closing.
Pseudocode here, doesn't remember the excact syntax of text area:
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With