Custom Java Text Swing JEditorPane

I have a list of objects (models) that are constantly being added (similar to a log file) and I would like to display as rich text in JEditorPane (view). How can I glue them together?

http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html#document does not seem to provide enough information to use.

+4
source share
4 answers

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)) 
+2
source

One simple solution would be to convert each object into a model in HTML and add lines to create an HTML document that can be installed on JEditorPane.

+2
source

Building a custom Abstract Document is painful. You are better off with an intermediate model that listens for changes both in your Object model and in the document (with DocumentListener ) and updates the model or view, depending. This works very well if you work in user time (as opposed to updating the Object model 1000 times per second).

0
source

OK, so the easiest way is to extend the JTextPane. The advanced class created and managed the base list. If you change the format (for example, new colors), the list will completely reformat the data. The only real problem was that autoscrolling is not 100% reliable, Both:

 Container parent = getParent(); // get the parent until scroll pane is found while (parent != null && !(parent instanceof JScrollPane)) { parent = parent.getParent(); } if (parent != null) { JScrollPane scrollPane = (JScrollPane)parent; scrollPane.getVerticalScrollBar().setValue(scrollPane.getVerticalScrollBar().getMaximum()); } 

and

 scrollRectToVisible(new Rectangle(0, getHeight() - 2, 1, 1)); 

Provide inconsistent results when the text panel sometimes does not scroll completely.

0
source

All Articles