Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从文档侦听器返回值_Java_Listener_Jtextpane_Documentlistener_Documentfilter - Fatal编程技术网

Java 从文档侦听器返回值

Java 从文档侦听器返回值,java,listener,jtextpane,documentlistener,documentfilter,Java,Listener,Jtextpane,Documentlistener,Documentfilter,我有两个java类。一旦将文档侦听器添加到文档(HTMLDoc)。另一个是实现DocumentListener的类 我希望能够向这个类返回一个值,这样我就可以知道文档什么时候被更改了,这样我就可以去掉不需要的html,这些html被粘贴进去并导致JTextPane出现问题 doc=(HTMLDocument)kit.createDefaultDocument(); //setContentType(“文本/html”); doc.addDocumentListener(新的ctextPanelL

我有两个java类。一旦将文档侦听器添加到文档(HTMLDoc)。另一个是实现DocumentListener的类

我希望能够向这个类返回一个值,这样我就可以知道文档什么时候被更改了,这样我就可以去掉不需要的html,这些html被粘贴进去并导致JTextPane出现问题

doc=(HTMLDocument)kit.createDefaultDocument();
//setContentType(“文本/html”);
doc.addDocumentListener(新的ctextPanelListener());
这是Listener类

public class CTextPaneListener implements DocumentListener
{

    // Gives notification that an attribute or set of attributes changed.
    @Override public void  changedUpdate(DocumentEvent e)
    {
        //System.out.println("DEBUG: changedUpdate() called");
    }

    //Gives notification that there was an insert into the document.        
    @Override public void insertUpdate(DocumentEvent e)
    {
        // I want to be able to return a value or a form a detection
        // so I can tell when there has been a insert.
    }

    //Gives notification that there was a remove from the document.                     
    @Override public void   removeUpdate(DocumentEvent e)
    {
        //System.out.println("DEBUG: removeUpdate called");
    }
}
我做过一点java,但已经有几年了,所以我有点生疏了。谢谢你抽出时间

编辑:这是我的自定义DocumentFilter,我原本以为这会捕获粘贴,但似乎只有DocumentListener能够捕获粘贴

public class CTextPaneFilter extends DocumentFilter
{
    public CTextPaneFilter(Document doc)
    {
        this(doc, 0);
    }

    public CTextPaneFilter(Document doc, int maxChars) {
        this.doc = doc;
        maxCharacters = maxChars;
    }       
    /**
    * Specifies the maximum text input length of the text pane.
    */
    public void setMaxLength(int len)
    {
        maxCharacters = len;
    }
    /**
    * Invoked prior to insertion of text into the specified Document. 
    */
    @Override public void insertString(DocumentFilter.FilterBypass fb, int offset,      String string, AttributeSet attr) throws BadLocationException
    {
    /**
    * Teuncates the inserted string so the contents
    * would be exactly maxCharacters in length.
    */
    System.out.println("insert");

    if (maxCharacters == 0 || (doc.getLength() + string.length()) <= maxCharacters) {
                        fb.insertString(offset, string, attr);
    } else {
        if (doc.getLength() < maxCharacters) {
            fb.insertString(offset, string.substring(0, maxCharacters - doc.getLength()), attr);
        }
    //Toolkit.getDefaultToolkit().beep();
    }
// other overridden methods below
公共类CTextPaneFilter扩展了DocumentFilter
{
公共CTextPaneFilter(文档文档)
{
这(doc,0);
}
公共CTextPaneFilter(文档文档,int-maxChars){
this.doc=doc;
maxCharacters=maxChars;
}       
/**
*指定文本窗格的最大文本输入长度。
*/
公共void setMaxLength(整数长度)
{
maxCharacters=len;
}
/**
*在将文本插入指定文档之前调用。
*/
@重写公共void insertString(DocumentFilter.FilterBypass fb,int offset,String String,AttributeSet attr)引发BadLocationException
{
/**
*t插入插入的字符串,以便
*长度正好是maxCharacters。
*/
系统输出打印项次(“插入”);

如果(maxCharacters==0 | | |(doc.getLength()+string.length())请看一看。它允许您在将文本写入文档之前重新格式化文本。

在发布之前格式化您的代码。如果我没有阅读它,并且我的权限编辑不起作用,我如何回答它。因此,我无法帮助您或其他人阅读它。您无法返回任何值(即使它不是一个接口)因为这将被称为系统,而且系统实际上知道有一个insert.MouseEvent,我也很害怕,你会建议如何解决这个问题?@Jeff实际上这个问题更适合回答那些喜欢
DocumentListener
的人,但在你的问题中,它显示得很糟糕。实际上,我有说到这里,由于某种原因,在JTextPane中,当文本通过键盘输入时,它无法识别插入或替换上的粘贴。我在上面添加了一些文本来解释为什么这对我不起作用。