我已经在 JTextPane 中注册了 Undoable Edit Listener 。
当编辑器套件保持不变时,效果很好。
但是当我打开另一个不同扩展名的文件时。我必须更改文本 Pane 编辑器套件。
注意:实际上textArea是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);
}
我发现当调用 setEditorKit 方法时注册文档监听器停止执行它的功能。
当我注册另一个可撤消的编辑监听器时,它将不起作用
textArea.setEditorKit(ek);
textArea.addUndoableEditListener(new MyListener());
最佳答案
I have to change the textpane editor kit
不,你不知道。
您只需使用:
textPane.setText("");
清除数据。
当然,如果您这样做,两个文件将使用相同的监听器。因此,无论如何,您希望每次更改文件时都替换编辑监听器。
关于java - 调用set editor kit方法时如何保留之前的Untenable Edit Listener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40179246/