Search code examples
javaswingtextjeditorpane

Java Swing custom text JEditorPane


I have a list of Objects (the model) that are constantly appended to (similar to a log file) and I'd like to display as rich text in a JEditorPane (the view). How can I glue them together?

http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html#document doesn't seem to give enough information to use.


Solution

  • You can use DefaultStyledDocument together with AttributeSet:

    SimpleAttributeSet attr = new SimpleAttributeSet();
    StyleConstants.setBold(attr , true);
    StyleConstants.setForeground(attr, Color.RED); 
    document.insertString(document.getLenght(),"yourstring", attr))