What JEditorPane event should be created for the listener?

Suppose I have a JEditorPane in a JPanel. I want to be able to call back every time the user types / inserts text into the JEditorPane component. What type of listener should I create?

+3
source share
3 answers

One way to do this is to create your own document and override the insertString method. For example:

class CustomDocument extends PlainDocument {
    @Override
    public void insertString(int offset, String string, AttributeSet attributeSet)
            throws BadLocationException {
        // Do something here
        super.insertString(offset, string, attributeSet);
    }
}

This allows you to find out what is inserted and veto it if you want (without calling super.insertString). You can apply this document using this:

editorPane.setDocument(new CustomDocument());
+3
source

DocumentListener .

, , , , , , PlainDocument.

JTextField, JTextArea, JEditorPane JTextPane. HTMLDocument, JTextPane StyledDocument. , , PlainDocument.

, DocumentFilter

+4

DocumentEvent , getOffset() getLength(), ,

,

+2
source

Source: https://habr.com/ru/post/1711853/


All Articles