|
Small code snippets with short descriptions.
| 04 Mar 2010 |
Refresh Views to show document's changes in JEditorPane. |
| Custom operations with Document structure or Document's elements attributes need to be reflected in Views. For such cases the following refresh() method was written. The refresh method generates a dummy event to notify document listeners about structure changes in given position and offset. Read more about refresh of JEditorPane's view> |
| 05 Feb 2010 |
Processing hyperlinks in editable JEditorPane with HTMLEditorKit. |
| Normally hyperlinks in JEditorPane with HTMLEditorKit are processed only when the JEditorPane isn't editable. Thus to let user click on such links or show hand cursor over them programmer must call setEditable(false) method. The solution is based on replacing HTMLEditorKit's original LinkHandler with a custom one. Read more to see how to overcome the editable state of JEditorPane> |
| 24 Dec 2009 |
Hanging (Negative) first line indent in JEditorPane. |
| There are two types of paragraph's first line indent: normal and hanging. Normal is easy achieved by specifying some positive value in appropriate attribute. For the hanging indent we have to specify not only some negative value but additionally equal positive left indent. When we do this in ParagraphView of StyledEditorKit we see some problems with select all and painting the par of first line in newly inserted paragraph. That could be fixed by changing ParagraphView behavior. Read more to see how hanging first line works in customized paragraph>
|
| 22 Dec 2009 |
Default TabStop size customization in JEditorPane. |
| By default java uses 72 pixels TabStop size which equals to 1 inch on paper. Sometimes it's necessary to change the default size but it's hardcoded in Paragraph. To change the size we have to change behaviour of nextTabStop() method. Read more to see how to customize default TabStop size>
|
| 07 Dec 2009 |
Merging elementaty UndoableEdits in one in JEditorPane. |
| By default elementary undoable edits are created for each Document's structure change e.g. for each typed letter. Undoing them one by one isn't very comfortable for user. It's much better if we group them somehow in bigger merged compound undoable edits and Undo them by groups. Read more to see how to merge the UndobleEdits in groups > |
| 04 Nov 2009 |
Vertical text fragment highlight in JTextArea. |
| To select vertical block of text typed in JTextArea it's necessary to create multiple selection fragments of the text. That can be achieved from custom caret.
Read more to see the example class for the caret> |
| 26 Oct 2009 |
Local images loading for HTML opened in JEditorPane. |
| Dynamic images in a HTML opened in JEditorPane can be used to show something without real access to disk or internet. Instead the images can be stored in e.g. a .jar file or created on fly. To let JEditorPane access them we can put them in cache used by HTMLEditorKit.
Read more to see how to put them in the cache> |
| 23 Oct 2009 |
Obtaining View's rectangle in JEditorPane. |
| Sometimes it's necessary to get view bounds e.g. to highlight it in parent component or to paint part of visible content. The following method returns the view bounds rectangle.
Read more to see the method code> |
| 21 Oct 2009 |
Using custom font (not registered) in StyledDocument of JEditorPane. |
| When a font isn't installed in OS but used from a file or as a resource from .jar library JEditorPane can't get it because font obtaining is based on list of fonts accessible from Toolkit.getFontList(). To provide access to such a font we can override getFont() method of Document used in the JEditorPane.
Read more about custom font access in Document> |
| 19 Oct 2009 |
Centering text vertically in JEditorPane. |
| To provide vertical alignment of JEditorPane's content root view must be replaced with a new one where children offsets recalculated properly.
Read more to see the working example > |
| 17 Oct 2009 |
JEditorPane's content height for fixed width. |
| Sometimes formatted content of JEditorPane or JTextPane should fill available width. The text is wrapped in several lines. To measure the necessary height we can use dummy component.
Read more to find how to get the height of the content > |
| 16 Oct 2009 |
Tab char measuring in paragraph with right or center alignment. |
When paragraph's alignment isn't left (e.g. right or center) tab char is represented as fixed width space rather than normal insets. The behavior is provided by javax.swing.text.ParagraphView class in the method
public float nextTabStop(float x, int tabOffset)
by lines
// If the text isn't left justified, offset by 10 pixels!
if(justification != StyleConstants.ALIGN_LEFT)
return x +10.0f;
Read more to find how to change the behavior >
|
|