【问题标题】:Return a value from a document listener从文档侦听器返回一个值
【发布时间】:2012-11-05 11:49:09
【问题描述】:

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

我希望能够向此类返回一个值,以便知道文档何时更改,以便我可以删除不需要的 html,这些 html 被粘贴并导致 JTextPane 出现问题。

doc = (HTMLDocument) kit.createDefaultDocument();
//setContentType("text/html");
doc.addDocumentListener(new CTextPaneListener());

这是监听类

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

【问题讨论】:

  • 在发布之前格式化您的代码
  • 连我都没有看怎么回答,我的权限编辑也不行。所以,我无法帮助您或其他人阅读它。
  • 你不能返回任何值(即使它不是一个接口),因为这将被系统调用,并且系统实际上知道有一个插入。
  • MouseEvent,我很害怕,你有什么建议可以解决这个问题?
  • @Jeff 其实这个问题更适合回答那些喜欢DocumentListener 的人,但在你的问题中,它的表现很糟糕。

标签: java listener jtextpane documentlistener documentfilter


【解决方案1】:

看看DocumentFilter。它允许您在将文本写入文档之前重新格式化文本。

【讨论】:

  • 我实际上已经对此进行了研究,由于某种原因,在 JTextPane 中它无法识别插入或替换上的粘贴,就在通过键盘输入文本时。
  • 我在上面添加了一些文字来解释为什么这对我不起作用。
猜你喜欢
  • 2021-04-07
  • 1970-01-01
  • 1970-01-01
  • 2013-02-16
  • 2013-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多