Text Utilities
Posted by Rob Camick on October 26, 2008
The JDK already supports a class dedicated to Text Utilities. Although you may have missed it because the class is called Utilities. The class is full of static methods to help you work with text components. Of course a few more methods would never hurt.
The RXTextUtilities class provides a few more static methods that you may find usefull, especially if you are building a simple text editor and want information for a status bar:
- centerLineInScrollPane
- getColumnAtCaret
- getLineAtCaret
- getLines
- gotoStartOfLine
- gotoFirstWordOnLine
- getWrappedLines
Although the above methods use a JTextComponent as a parameter, the methods have only been tested using a JTextArea and JTextPane with plain text. I have no idea what results you would get using a text component with HTML.
Also, the getColumnAtCaret() method only makes sense when using a Monospaced font.
Many of the methods take advantage of the Element structure used by the Document of the text component to find the required information.
Adam said
Helpful indeed, cheers
Rob Camick said
Glad you find some of the methods usefull.
Graham said
Thanks so much Rob for making available your utilities. I’m relatively new to Java coding and learn much more from working examples than trial & error. Your method centerLineInScrollPane has no given me a much better understanding of how to use setViewPosition.
Rob Camick said
Glad the examples help. I also find the learning by example is a great way to learn and experiment with new classes.
jnorthr said
thanx for your efforts. it’s a joy to find something so easy to integrate into a new codebase without re-inventing the wheel !
Rob Camick said
Thanks, integrating into any codebase is one of the goals of the majority of the posts.
Matvey said
It looks great! But how can I use this. How to import it? Could you please help me