简体   繁体   English

自己插入LeafElement

[英]Inserting a LeafElement myself

I am trying to insert a LeafElement inside an overriden DefaultStyledDocument (also providing a custom EditorKit) for a JEditorPane : but I get a javax.swing.text.StateInvariantError. 我试图在JEditorPane的重写DefaultStyledDocument(还提供自定义EditorKit)的内部插入LeafElement:但是我得到了javax.swing.text.StateInvariantError。 So following the guidelness of a JGuru forum topic , I added writeLock() and writeUnlock() calls, but this time my JEditorPane remains empty. 因此,遵循JGuru论坛主题的指导 ,我添加了writeLock()和writeUnlock()调用,但是这次我的JEditorPane仍然为空。 So how should I override the fireXXX() methods of AbstractDocument, in order to notify all listeners of the document ? 那么,为了通知文档的所有侦听器,我应该如何重写AbstractDocument的fireXXX()方法?

Here my main class : MyFrame.java 这是我的主类: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;

}

Here my class MyEditorKit.java 这是我的课程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;


}

And my class MyDocument.java 还有我的课程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;

}

This is the error stacktrace when I Comment the lines with writeLock() and writeUnlock() : 当我用writeLock()和writeUnlock()注释行时,这是错误stacktrace:

    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)

Thanks in advance. 提前致谢。

I finally solved my problem : 我终于解决了我的问题:

Just changes the MyDocument.java to this : 只需将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;

}

Why these added lines ? 为什么要增加这些行?

Well the JGuru topic links suggest that a new DocumentEvent should be generated : in my case, it is an insert event. 那么,JGuru主题链接建议应该生成一个新的DocumentEvent:对于我来说,这是一个插入事件。 So : 因此:

  1. I create this DocumentEvent instance 我创建了这个DocumentEvent实例
  2. In insert a string to the content of the Document (otherwise, it won't grow : so mandatory even if I'm just using a JComponent) => getContent().insertString(offset, String) 在文档内容中插入字符串(否则,它将不会增长:即使我只是使用JComponent也是如此)=> getContent()。insertString(offset,String)
  3. I use it in order to apply changes in the document (insertUpdate()) 我使用它来应用文档中的更改(insertUpdate())
  4. I mark it as "not in progress any more" (end()) 我将其标记为“不再进行中”(end())
  5. I notify all document listeners (fireInsertUpdate()) 我通知所有文档侦听器(fireInsertUpdate())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM