File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Highlighting Text Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Highlighting Text" Watch "Highlighting Text" New topic

Highlighting Text

Jeff Storey
Ranch Hand

Joined: Oct 09, 2006
Posts: 118
Hi. I currently use a JTextArea to display my text and I can highlight certain portions of it using a the Highlighter class. However, when I switch to a JEditorPane and display HTML I run into some problems.

When I highlight the text, I do it based on the index of the characters (for example, highlight characters 10-20). When I switched to the JEditorPane, these highlights do not highlight the same places any more (even when I don't have any HTML being displayed). Can someone explain to me why this is happening and how I might go about highlighting the correct indices in the JEditorPane?

Thank you.
Jeff Storey
Brian Cole
Ranch Hand

Joined: Sep 20, 2005
Posts: 878
It's kind of hard to answer without more information. You might post some code.

I'm kind of confused why you mention HTML here. JEditorPane can display HTML (using the HTMLEditorKit) but can also display other kinds of formatted text. If you don't need to actually mess with HTML or RTF or something like that, you may want to use a JTextPane instead. (JTextPane happens to be a subclass of JEditorPane.)

bitguru blog
I agree. Here's the link:
subject: Highlighting Text
It's not a secret anymore!