Java Swing - Detecting a change in the document

886 views Asked by At

For school, I'm attempting to recreate Microsoft's Notepad program using Java's Swing. I'm working on the saving and opening of .txt files, and I'm trying to figure out a way for the program to detect when a change has been made to the document. If a change has been detected and the user chooses to open or create a new file, I want the program to prompt the user if they would like to save their changes before continuing.

My thought for this was to create a flag, called documentChanged, that would initially be false, and which would be set to true whenever a change was made to the JTextArea. To detect this change, I thought of using a TextListener as follows:

public class JNotepad implements ActionListener
{
    boolean documentChanged;
    JTextArea notepad;

    JNotepad()
    {
        documentChanged = false;

        notepad = new JTextArea();
        notepad.addTextListener(new TextListener() {
            public void textValueChanged(TextEvent te) {
                documentChanged = true;
            }
        });
    }
}

However, I learned that Java classes are unable to implement multiple interfaces at once, and I'm already using ActionListeners to implement the items of my notepad's menu bar.

My question is, is there any way to use both TextListener and ActionListener (or any other listener) simultaneously in the same class? If not, what would be my best plan of action for detecting a change in the document?

2

There are 2 answers

3
Hovercraft Full Of Eels On BEST ANSWER

How does your this even compile

notepad = new JTextArea();
notepad.addTextListener(new TextListener() {
   // ....
}

since TextListeners are not defined to work with JTextAreas but rather with TextAreas, a completely different beast.

You should add a DocumentListener to your JTextArea's Document.

notepad.getDocument().addDocumentListener(new DocumentListener() {
    void insertUpdate(DocumentEvent e) {
        documentChanged = true;
    }

    void removeUpdate(DocumentEvent e) {
        documentChanged = true;
    }

    void changedUpdate(DocumentEvent e) {
        documentChanged = true;
    }

});

Regarding

My question is, is there any way to use both TextListeners and ActionListeners (or any other listener) simultaneously in the same class?

Use of a DocumentListener has nothing to do with ActionListeners used elsewhere in your program since their domains are orthogonal to each other, i.e., the one has absolutely nothing to do with the other.

1
Gaara On

It was answer in another post. See Text Changed event in JTextArea? How to?

And also see How to Write a Document Listener (DocumentListener) in Oracle, you will see an applet example.