在TextArea,Java中使用Document Listener时出现java.lang.IllegalStateException [英] java.lang.IllegalStateException while using Document Listener in TextArea, Java

查看:143
本文介绍了在TextArea,Java中使用Document Listener时出现java.lang.IllegalStateException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DocumentListener dl = new MessageDocumentListener();
((AbstractDocument) nboxArea.getDocument()).setDocumentFilter(new DocumentFilter() {
    public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
        string = string.replaceAll("\t", "");
        super.insertString(fb, offset, string,(javax.swing.text.AttributeSet) attr);
    }

    public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
        text = text.replaceAll("\t", "");
        //TODO must do something here
        super.replace(fb, offset, length, text,(javax.swing.text.AttributeSet) attrs);
    }
});

JTextArea evArea = (JTextArea) c;
evArea.getDocument().removeDocumentListener(dl);
evArea.setText(originalMessage);

在这种情况下,我在textarea中设置文本时发现了以下错误。我不知道如何解决。

In this case I found the following error during set text in textarea. I do not know how to resolve.

Exception in thread "AWT-EventQueue-0" 
java.lang.IllegalStateException: Attempt to mutate in notification

我认为问题是在文档中设置文本或在文档中设置文档文件听众。但我不知道如何解决这个问题。请帮我解决这个问题。

I think the problem is to set text in document or setting document in document listener. But I do not know how to solve this. Please help me to solve this issue.

推荐答案

您无法修改DocumentListener中的文档。编写一个自定义文档,它会覆盖insertString()或remove()方法。

You cannot modify the document inside the DocumentListener. Write a custom Document instead, which overrides the insertString() or remove() methods.

来自Java教程:如何编写DocumentListener


文档监听器不应修改文档的内容;在收听者收到更改通知时,更改已完成。而是编写一个自定义文档来覆盖insertString或删除方法,或两者​​兼而有之。有关详细信息,请参阅收听文档更改

这篇关于在TextArea,Java中使用Document Listener时出现java.lang.IllegalStateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆