So I had a JTextPane that displays text in HTML format. The color issue arises when I disable the JTextPane. A workaround to this is to use the setEditable() method instead of setEnabled().

Then again, another issue arises. You will be able to select the text even though you cannot insert or remove characters in the JTextPane. If you do not want the JTextPane to have a selection feature if it is not editable, then only way to make it work is to override its protected method processMouseEvent().

This should do the trick.

Converting a mouse position to the caret position of a JTextComponent can be done by using the JTextComponent method viewToModel(). This can be pretty useful if you need to set the caret position of the component when you do a right click of the mouse.

The component can apply to JTextArea, JEditorPane and JTextPane respectively.

The image in this post shows how the highlight matching code works with a JTextPane. But actually, the code uses a JTextComponent so either a JEditorPane, JTextArea or JTextPane will work with this code.

Type in the word that you want to find, then press the FIND button and it will highlight all words matching it. The highlight matching is case insensitive so if you want to make it case-sensitive, just remove the part of the code where toLowerCase() is called.

To use the class, just do it like this:

And here is the source code for the WordEditorSearch class.

Related Posts Plugin for WordPress, Blogger...