DocumentFilter和替换方法调用新的线程?

时间:2016-06-14 17:23:05

标签: java multithreading jtextpane documentfilter

我对DocumentFilter类的方法replace()有疑问。 这种方法很慢。我想在一个新线程中调用此方法来加速执行...但我得到以下异常

我的代码与

相同



public void replace(final FilterBypass fb, final int offset, final int length, final String text, final AttributeSet attrs) throws BadLocationException
  {
super.replace( fb, offset, length,text, null) ;
while( elem : vectorToken)
  new Thread()
	{
	  public void run() 
      {
        super.replace( fb, elem.offset(), elem.length(),elem.text(),elem.new_attrb()) ;
	  }
    }.start();	   	   
  }




我得到了这个例外



Exception in thread "Thread-2" javax.swing.text.StateInvariantError: Illegal cast to MutableAttributeSet
	at javax.swing.text.AbstractDocument$AbstractElement.checkForIllegalCast(Unknown Source)
	at javax.swing.text.AbstractDocument$AbstractElement.addAttributes(Unknown Source)
	at javax.swing.text.AbstractDocument$AbstractElement.<init>(Unknown Source)
	at javax.swing.text.AbstractDocument$LeafElement.<init>(Unknown Source)
	at javax.swing.text.AbstractDocument.createLeafElement(Unknown Source)
	at javax.swing.text.DefaultStyledDocument$ElementBuffer.insertFirstContent(Unknown Source)
	at javax.swing.text.DefaultStyledDocument$ElementBuffer.insertUpdate(Unknown Source)
	at javax.swing.text.DefaultStyledDocument$ElementBuffer.insert(Unknown Source)
	at javax.swing.text.DefaultStyledDocument.insertUpdate(Unknown Source)
	at javax.swing.text.AbstractDocument.handleInsertString(Unknown Source)
	at javax.swing.text.AbstractDocument.access$200(Unknown Source)
	at javax.swing.text.AbstractDocument$DefaultFilterBypass.replace(Unknown Source)
	at javax.swing.text.DocumentFilter.replace(Unknown Source)
&#13;
&#13;
&#13;

请问我该如何解决这个问题。 谢谢

0 个答案:

没有答案