M.H
M.H

Reputation: 3

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());

Upvotes: 0

Views: 237

Answers (2)

M.H
M.H

Reputation: 3

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);

Upvotes: 0

camickr
camickr

Reputation: 324157

I have to change the textpane editor kit

No you don't.

You just use:

textPane.setText("");

to clear the data.

Of course if you do this the same listener would be used for both files. So you want to replace the edit listener every time you change the file anyway.

Upvotes: 1

Related Questions