Articles Projects Tips Downloads Contacts About

Small code snippets with short descriptions.

15 Feb 2013 Fix for java 7 text wrapping bug.
Java 7 introduced a new concept of wrapping. The concept seems to have some holes in the implemetation. Then wrapping of text chunks with different attributes (styled fragments) works incorrectly Read more about the wrap fix>

31 Jan 2013 Fix for wrap difference java 6 vs java 7 (letter wrap).
Java 7 shows wrap behaviour difference in JEditorPane/JTextPane. From forums: In all versions of Java up to 6, the default behaviour of a JTextPane put inside a JScrollPane was: wrap lines at word boundaries if possible. If not, then wrap them anyway. Read more about the wrap behavior fix>

06 Apr 2012 Multi column flow layout (Windows Explorer like).
If we would like to have layout manager which works like Windows Explorer in folder view when items flow but organized in columns the example could help. Read more about the flow column layout>

13 Mar 2012 FlatteningPathIterator and moving object along Shape path.
To move object along path defined by a custom Shape we can use FlatteningPathIterator based approach. From javadoc 'The FlatteningPathIterator class returns a flattened view of another PathIterator object. Other Shape classes can use this class to provide flattening behavior for their paths without having to perform the interpolation calculations themselves.' Read more about moving object along shape based path solution>

01 Mar 2012 Disabling word selection by double click in JTextComponent.
The requirement I met on forums and I think it could be useful. The selection is done by Caret so I just override DefaultCaret mouse events processing methods to skip the word selection code. The solution works for JEditorPane and JTextArea. Read more about the disabling word selection solution>

18 Nov 2011 Multiple floatable JToolBars for JFrame.
By default java allows only one floatable JToolBar for JFrame and it floats only if BorderLayout is set. The solution provides BorderLayout extension to let user add multiple components to all sides. Read more about the multiple floatable toolbars solution>

03 Sep 2011 Changing JLabel's font size to fill all available space.
A useful GUI element is a JLabel which resizes its font to fill all available space. On each resize we just check possible bounds of the lable's text to fit available area. Read more about the JLabel extension>

10 Aug 2011 Slider to rotate content of Container.
If a Container with CardLayout contains multiple components and we want to add a bit more dynamic. To show next or previous component we move the old component out and replace it with a new one. Read more about sliding component>

21 Jun 2011 Form submitting (method=POST) in JEditorPane with HTMLEditorKit.
If we want to use JEditorPane as a limited browser we often need to submit form sending the form's data to server and obtain results. Read more about submitting form in JEditorPane>

21 Mar 2011 Colored Strikethrough text attribute with user defined color in JEditorPane.
To implement coloring of strikethrough attribute we define a custom attribute with name "strike-color" in the StyledEditorKit extension. The attributes value is necessary Color. Read more about coloring strikethrough in JEditorPane>

14 Dec 2010 Loosing last empty paragraph in HTML from JEditorPane fix.
JTextPane with HTMLEditorKit set ignores/removes empty last empty paragraphs when we try to get html content via getText() method. The behavior comes from the protected boolean inRange(Element next) method of the HTMLWriter. Read more about loosing last paragraph in JEditorPane fix>

17 Nov 2010 Letter wrap in html in JEditorPane.
HTML in browsers provide space based wrap support. Alternatively in custom layout we may need a letter based wrap behaviour. In other words content should be wrapped in the mid of word not by space. Read more about letter wrap for html in JEditorPane>

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>

09 Dec 2009 Autocreation links from user typed text in JEditorPane with HTMLEditorKit.
A good practice in editors is to create links automatically as soon as user typed it (e.g. some text started from "www" or "http" ). Read more to see how to add the auto conversion feature >

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 >

03 Dec 2009 Autoreplace smiles text e.g :) with appropriate images in JEditorPane.
Implementing chat or messenger application we often need something like this. That's a small funny feature based on DocumentListener and StyledEditorKit to support images. Read more to see the example of autoreplacing typed text with images >

27 Nov 2009 Overwrite/Insert modes switching by INSERT key in JEditorPane.
Normally the overwrite mode is used in editors to replace existing text during typing. When when the mode is ON newly typed text replaces previous text. It has visible for user effect showed as rectangle caret shape. Read more to see the example application of modes switching by INSERT key>

16 Nov 2009 Merge two Documents' content in one.
The code can be useful when we need to merge to DefaultStyledDocument with all structures and attributes e.g. when single message must be added in a list Read more to see the merge method and example application to see how it works>

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>

20 Oct 2009 Row and column of caret in JTextComponent.
To calculate row and column numbers for current caret position in any of JEditorPane, JTextPane and JTextArea we can use javax.swing.text.Utilities class. Read more to see getRow() and getColumn() functions >

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 >