javaswingjtextpanedocumentlistener

How to retain previous Undoable Edit Listener When set editor kit mehtod is called


I have registered Undoable Edit Listener in JTextPane .

It works nice when editor kit remains unchanged.

But When i am opening another file of different extension. I have to change the textpane editor kit .

Note: Actually textArea is an instance of JTextPane

   if(ext.equals(".txt")){
try{
   // textArea.setText(null);
textArea.setEditorKit(defaultkit);

    String read=reader.readLine();
StringBuilder text = new StringBuilder();
    int offset =0;
  //int test=0;
     while (read  !=null){
text.append(read);
text.append('\n');
read = reader.readLine();


    }
textArea.setText(text.toString());

}catch(IOException e ){}
//catch(BadLocationException e ){}

 }
 else{

     try {
 FileInputStream fis = new FileInputStream(path);    


 if(ext.equals(".rtf")){


 textArea.setEditorKit(ek);
 textArea.getEditorKit().read(fis, textArea.getDocument(), 0);


     }

I've found that when setEditorKit method is being called register document listener stop performing it's function .

When I register another undoable edit listner it won't work

textArea.setEditorKit(ek);
textArea.addUndoableEditListener(new MyListener());

Solution

  • I found the problem and its solution

    i was using new instance of document listener while registering another document listener

    MyDocumentListener dl = new MyDocumentListener();
    textArea.getDocument().addUndoableEditListener(new MyDocumentListener());
    

    instead i should use this every time:

    textArea.getDocument().addUndoableEditListener(dl);