我试图在一个覆盖DefaultStyledDocument(也提供自定义EditorKit)为JEditorPane插入一个LeafElement:但我得到一个javax.swing.text.StateInvariantError。因此,按照JGuru论坛主题的指导方针,我添加了writeLock()和writeUnlock()调用,但这次我的JEditorPane仍然是空的。那么,我应该如何重写AbstractDocument的fireXXX()方法,以便通知文档的所有侦听器?
这里是我的主类:MyFrame.java
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
public class MyFrame extends JFrame {
public MyFrame(){
setSize(400, 300);
JEditorPane editorPane = new JEditorPane();
editorPane.setEditorKit(new MyEditorKit());
add(new JScrollPane(editorPane));
}
public static void main(String[] args) {
new MyFrame().setVisible(true);
}
private static final long serialVersionUID = -2122161377842820073L;
}
这里是我的类MyEditorKit.java
import javax.swing.text.Document;
import javax.swing.text.StyledEditorKit;
public class MyEditorKit extends StyledEditorKit {
@Override
public Document createDefaultDocument() {
return new MyDocument();
}
private static final long serialVersionUID = -5973765338689236766L;
}
我的类mydocument。java
import javax.swing.JLabel;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
public class MyDocument extends DefaultStyledDocument {
public MyDocument(){
BranchElement rootElement = (BranchElement) getDefaultRootElement();
writeLock();
LeafElement black10Element = (LeafElement) createLeafElement(rootElement, new SimpleAttributeSet(),0, 1);
JLabel black10 = new JLabel("10 ...");
StyleConstants.setComponent(black10Element, black10);
writeUnlock();
}
private static final long serialVersionUID = -7424640574620960694L;
}
这是当我用writeLock()和writeUnlock()注释行时的错误堆栈跟踪:
Exception in thread "main" javax.swing.text.StateInvariantError: Illegal cast to MutableAttributeSet
at javax.swing.text.AbstractDocument$AbstractElement.checkForIllegalCast(AbstractDocument.java:2050)
at javax.swing.text.AbstractDocument$AbstractElement.addAttributes(AbstractDocument.java:1983)
at javax.swing.text.AbstractDocument$AbstractElement.<init>(AbstractDocument.java:1777)
at javax.swing.text.AbstractDocument$LeafElement.<init>(AbstractDocument.java:2502)
at javax.swing.text.AbstractDocument.createLeafElement(AbstractDocument.java:1275)
at MyDocument.<init>(MyDocument.java:13)
at MyEditorKit.createDefaultDocument(MyEditorKit.java:9)
at javax.swing.JEditorPane.setEditorKit(JEditorPane.java:1058)
at MyFrame.<init>(MyFrame.java:11)
at MyFrame.main(MyFrame.java:16)
我终于解决了我的问题:
只是把MyDocument.java改成这样:
import javax.swing.JLabel;
import javax.swing.event.DocumentEvent.EventType;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
public class MyDocument extends DefaultStyledDocument {
public MyDocument(){
BranchElement rootElement = (BranchElement) getDefaultRootElement();
writeLock();
LeafElement black10Element = (LeafElement) createLeafElement(rootElement, new SimpleAttributeSet(), 0, 1);
JLabel black10 = new JLabel("10 ...");
StyleConstants.setComponent(black10Element, black10);
/////////////////// Added lines
DefaultDocumentEvent documentEvent = this.new DefaultDocumentEvent(0, 1, EventType.INSERT);
try {
getContent().insertString(0, " ");
} catch(BadLocationException e){
e.printStackTrace();
}
insertUpdate(documentEvent, black10Element);
documentEvent.end();
fireInsertUpdate(documentEvent);
/////////////////////////////////////
writeUnlock();
}
private static final long serialVersionUID = -7424640574620960694L;
}
为什么要添加这些行?
JGuru的主题链接建议应该生成一个新的documenttevent:在我的例子中,它是一个插入事件。所以:
- 我创建了这个DocumentEvent实例
- 在插入字符串到文档的内容(否则,它不会增长:所以强制,即使我只是使用一个JComponent) => getContent()。插入字符串(offset, String)
- 我使用它是为了在文档中应用更改(insertUpdate())
- 我将其标记为"not in progress any more" (end())
- 我通知所有的文档监听器(fireInsertUpdate())