Search code examples
javaswingjtextpanejtabbedpanedocumentlistener

Quirk with my text editor app and DocumentListener


I have a JTabbedPane. I add tabs based upon an instantiation of a class (herein known as textWrap) that has the info for a JTextPane. I create a new instance of the listener and pass that into the instantiation of the textWrap:

tabEditorPane.addTab( fileName, new TextEditorWrap( new MyDocumentListener() ... ...);

My DocumentListener adds an asterisk to the tab when the file changes. This all seems ok until I try to load an existing file into a text tab. The listener registers the initial BufferedReader input and adds an asterisk. How Can I get around this?

Is there a rule of thumb for this thing? I realize I could register the listener after the class is created, as to after the text file is loaded, but I want to see what the stack says.


Solution

  • When you load the file, there should be a call like

    myTextPane.getEditorKit().read(...);
    

    Or any equivalent loading method. What you can do is replace this line by 4 steps :

    1. Remove or deactivate your document listener
    2. load your file
    3. remove the asterisk
    4. Put back, or reactivate the document listener.

    To do so, you can, for instance, extend your text pane and write a load method that will do these 4 steps. Anyway, the object that will manage the file loading will need a reference to your DocumentListener