Constructing a DocumentPane

Simply creating a new instance of an editor pane and throwing it onto a JPanel is not enough to display and use a JEditorPane. Due to the relationship of Document, EditorKit, JEditorPane and StyleSheet it is necessary to initialize all elements properly.

In DocumentPane's basic constructor DocumentPane() a new J EditorPane is created and assigned to field editor. The caret color for the editor pane is set and the typical text cursor is assigned by calling method setEditCursor (see below).

Then a new SHTMLEditorKit is created and assigned to our editor pane. This procedure ensures that a document gets correctly initialized with the style sheet properly attached.

Finally a new JScrollPane is created, the editor pane is added to the scroll pane and both are added to the DocumentPane after defining an appropriate layout.

The editor pane has to reside inside a JScrollPane for a vertical scroll bar automatically being shown as needed. The editor pane automatically wraps words at the right end of the pane so that a horizontal scroll bar is not shown or needed.

Method setEditCursor

Although JEditorPane has a method setCursor inherited from java.awt.Component, setting the cursor with that method does not cause respective cursor to be displayed (probably someone can let me know why sometime). Method setEditCursor therefore adds a MenuListener to our editor, that reacts on mouseEntered and mouseExited events.

When the mouse enters the editor pane, the cursor is set to the text cursor, when the mouse exits the editor pane the cursor is reset to a default cursor.

Method setEditCursor achieves this by getting the glassPane of the DocumentPane's JRootPane, assigning respective cursor and setting the glassPane to visible.

Constructing a DocumentPane with above steps sets up the basic contents of a DocumentPane. Read on to learn how to create a new document for editing or to open an existing one.