Converting plain text into embedded hyperlink

I'm attempting to output text whereby the majority of it will be simple plain text, but I would like some sentences to be converted to a hyperlink which when clicked will allow me to display a document held in a database.

What would be the best way of accomplishing this? Basically every sentence has a flag set, when I process each sentence I check the flag and based on this I would like to display as is or as a hyperlink.

At the moment I am outputting in a JTextPane but can change that if required.

Thanks in advance. :)

EDIT: Just thought I'd clarify a little - I don't need the text to be a hyperlink in the traditional sense of the word but merely for the text to be clickable, allow the program to recognise which text passage has been clicked, and act appropriately.

Message was edited by:

thoseion

[855 byte] By [thoseiona] at [2007-11-27 3:51:34]
# 1
As long as you know what link a certain sentence corresponds to, it should be simple. A data structure such as HashMap would be very helpful in this case.
kdajania at 2007-7-12 8:55:34 > top of Java-index,Java Essentials,Java Programming...
# 2
> As long as you know what link a certain sentence> corresponds to, it should be simple. A data structure> such as HashMap would be very helpful in this case.And how would a HashMap generate a hyperlink?
georgemca at 2007-7-12 8:55:34 > top of Java-index,Java Essentials,Java Programming...
# 3
Some magic I presume. I can usually command electricity to do certain things for me...I meant that is how the look up can be made. As for the display, I don't know about swing, HTML would be easy though. Why don't you share George?
kdajania at 2007-7-12 8:55:34 > top of Java-index,Java Essentials,Java Programming...
# 4

> Some magic I presume. I can usually command

> electricity to do certain things for me...

>

> I meant that is how the look up can be made. As for

> the display, I don't know about swing, HTML would be

> easy though. Why don't you share George?

Oh, another one of those. Just what the boards need, someone else who doesn't like to be corrected

Use a JEditorPane to embed HTML in. Unless, of course, you want to use magic

georgemca at 2007-7-12 8:55:34 > top of Java-index,Java Essentials,Java Programming...
# 5

No, I don't mind being corrected. I was in the process of trying to edit my message to include that info when I saw your post. I just thought it was funny you were quick to correct and not supply an answer. We are all here trying to help, not argue, so why don't we do just that :)

Message was edited by:

kdajani

kdajania at 2007-7-12 8:55:34 > top of Java-index,Java Essentials,Java Programming...
# 6

Thanks for the reply.

I've looked into the JEditorPane and there's a couple things about it that I'm not sure about - how to append text and along with it, how to append hyperlinks.

Almost all the examples I've seen on it are for reading in a full HTML file at initiation.

I have attempted the following code which appears to work OK as far as appending text goes, but if the amount of text being added is very large, I get an out of memory error:

public void setBetweenText(String sentence) {

Document doc = (Document)txtBetween.getDocument();

try{

AttributeSet attr = ((HTMLEditorKit) txtBetween.getEditorKit()).getInputAttributes();

doc.insertString(doc.getLength(), sentence, attr);

}catch

(BadLocationException e){

}

}

As for converting any of those sentences into hyperlinks, I can't find any examples of how to do that. I've attempted something like the following just to see if it'd work but to no avail:

sentence = "<a href="+path+">"+sentence+"</a>";

Are there simple solutions to these problems or is there another way to accomplish what I'm trying to do. To reiterate, I don't actually need hyperlinks per-se, just a way for me to output some text strings, highlight specific ones so the user knows which are selectable, and then allow the user to select the string. I thought a hyperlink would be most suitable but if there is something else then I am all ears.

thoseiona at 2007-7-12 8:55:34 > top of Java-index,Java Essentials,Java Programming...