David Kaminsky
David Kaminsky

Reputation: 87

How would I create a text editor in Java that allows me to annotate text?

I am working on a project that contains a feature in a JEditorPane or similar that allows for the highlighting of certain areas of text and adding "annotations" for that text. These annotations would be similar to a comment. I would also try to include tooltips that show the actual text of the annotation

Example:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

I would highlight the first set of text "consectetur adipisicing elit," add an annotation which would let me set a highlight color and add a tooltip with a comment. The same would happen with "ullamco laboris nisi ut aliquip" except that the color and comment would be different.

The editor should be able to display these multiple highlightings.

Can anyone point me in the right direction? Is there any library I can use to accomplish this easier?

Thanks,

David

Upvotes: 2

Views: 450

Answers (1)

DNA
DNA

Reputation: 42597

For any JTextComponent (JTextField, JTextPane, JEditorPane etc) you can use setHighLighter() and add highlights to it to indicate the text spans where your annotations belong. You can change the colour using different HighlightPainter objects.

You could also set the tooltip text according to whether the mouse is over a highlight (annotation) by over-riding getToolTipText() and using viewToModel() to determine where in the text the mouse is pointing.

Upvotes: 1

Related Questions