powered by Jive Software

HyperlinkListener: can do in JTextPane?

Hi all,

in top section of my chat window, where messages come in, I have a JTextPane… is it possible to do a HyperlinkListener in a JTextPane? I hear it has to be a JTextEditor, that you have to use HTMLEditorKit, is this right? I would have to start from scratch, since am using JTextPane and use styles for the different font colors… for example:

doc = textAreaTop.getStyledDocument();

StyleConstants.setForeground(msgBody, fontColorMyMsg);

msgBodyIn = textAreaTop.addStyle(“d”, null);

doc.insertString(doc.getLength(), iMsgS, msgBody);

etc…

I have this code for HyperlinkListener, which compiles fine, but does not do the trick:

URL url;

public void hyperlinkUpdate(HyperlinkEvent he) {

if (he.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {

url = he.getURL();

System.out.println(url.toString()); // prints url ok…

if (he.getEventType() == HyperlinkEvent.EventType.ENTERED) {textAreaTop.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

}

if (he.getEventType() == HyperlinkEvent.EventType.EXITED){

textAreaTop.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));

}

getAppletContext().showDocument(url, “_blank”);

}

}

I would very much appreciate some help here, as to how to implement HyperlinkListener in JTextPane (if this is at all possible…) I know this is not strictly a Smack question, but I figured this would still be a good place to ask since, well, plenty of folks here must be doing HyperlinkListeners… thank you very much…

You need to you the HTMLEditorKit and make sure the links are tagged as hyperlinks inside of the document, so you need to use the insert html method.

Alex

yikes!! from docs:

public void insertHTML(HTMLDocument doc,

int offset,

String html,

int popDepth,

int pushDepth,

HTML.Tag insertTag)

throws BadLocationException,

IOException

so if a user sends a message that reads:

“visit http://www.yahoo.com and tell me what you think”

how would I do this? I mean how would I incorporate that into this:

docTop.insertString(docTop.getLength(), sUid, uid);

docTop.insertString(docTop.getLength(), iMsgS, msgBody);

how do you break down msg betw. ‘‘plain’’ text and url?

thank you very much…

what i do is after adding it to the textpane iniatially I go back and search for any links inside of what i just inserted. If there is one I reinsert it as html.

you “search” for any links? after msg is sent (or before it’‘s sent) you search for a string that starts with “http” and ends with “.com”? or “org”? or “.net” or “edu”? what if they don’'t type a space before “http:”? what if they start url with only “www”? I would appreicate seeing some code here… this has given me considerable trouble… am also trying to do it w/JEditorPane (have two versions of my applet now, one w/JTextPane, one w/JEditorPane… will go w/whatever I can make work… hopefully I can do diff. font colors w/JEditorPane also…)

but in JEditorPane can’'t even get setText() to work:

String sUid = "UserName: ";

String iMsgS = textAreaBottom.getText();

System.out.println(iMsgS + " – message"); // prints fine in console…

iMsgS = iMsgS + “\n”;

textAreaTop.setText(iMsgS); // doesn’'t print

textAreaTop.setText(“message sent” + “\n”); // prints fine…

(this is not applet going thru server and such, it’‘s a separate applet I’'m using just to test this… for now just trying to grab msg typed in bottom area and print it in top area…)

what is equivalent of “append()” method for JEditorPane? thank you very much…

ok, I see what issue was with that… can get msg to print now… (but still need to find a method that does what append() does…) now have:

JEditorPane textAreaTop = new JEditorPane();

textAreaTop.setContentType(“text/html”);

HTMLEditorKit edKit = new HTMLEditorKit();

textAreaTop.setEditorKit(edKit);

textAreaTop.setEditable(false);

// …

String sUid = "User: ";

String iMsgS = textAreaBottom.getText();

iMsgS = iMsgS + “\n”;

textAreaTop.setText(iMsgS);

// …

URL url;

public void hyperlinkUpdate(HyperlinkEvent he) {

if (he.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {

url = he.getURL();

if (he.getEventType() == HyperlinkEvent.EventType.ENTERED) {

textAreaTop.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

}

if (he.getEventType() == HyperlinkEvent.EventType.EXITED){

textAreaTop.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));

}

getAppletContext().showDocument(url, “_blank”);

}

}

if I type a url in bottom area I see it fine on top, but it’'s not a link… how can I make it a link, pls… thank you…

Here is the code to add a link:

kit.insertHTML(doc, pos,

“”, 0, 0,

HTML.Tag.A);

/code

where sub is the link

thank you very much, AWenckus… ended up doing w/AttributeSet, that way was able to keep my StyledDoc code…

What do you mean? If you found a better/different way of doing it let us all know so we can be enlightened.

I will… when I have time, am so busy right now… you folks have been so helpful, it’'s the least I can do…

(and still haven’‘t finished, need to search msg for url w/a reg expression, loop in case there’‘s more than one url in msg, etc… you might find it’'s more trouble than you want to go thru, but I did want to do it in JTextPane keeping my StyledDoc code…)

when I’'m done will post my code… and you can decide if you like it or not… again, many thanks for all yr help…

A Wenckus… here’‘s my entire getMsg() method… which includes hyperlinks for urls and for e-mail addresses… mind seem more trouble than it’'s worth to some, but as I mentioned earlier really wanted to do it w/JTextPane and StyledDoc (and I use Attributes for links…)

public void getMsg() {

doc = textAreaTop.getStyledDocument();

uid = textAreaTop.addStyle(“a”, null);

StyleConstants.setForeground(uid, fontColorUid);

uidIn = textAreaTop.addStyle(“b”, null);

StyleConstants.setForeground(uidIn, fontColorUidInc);

msgBody = textAreaTop.addStyle(“c”, null);

StyleConstants.setForeground(msgBody, fontColorMyMsg);

msgBodyIn = textAreaTop.addStyle(“d”, null);

StyleConstants.setForeground(msgBodyIn, fontColorIncMsg);

SimpleAttributeSet linksAttr = new SimpleAttributeSet();

Color fontColor = new Color(0,0,225);

StyleConstants.setForeground(linksAttr, fontColor);

StyleConstants.setUnderline(linksAttr, true);

Message iMsg;

// get incoming messages

while ((iMsg = EventChat.pollMessage()) != null) {

String Sender = StringUtils.parseResource(iMsg.getFrom());

String sUid = Sender + ": ";

// insert Uid into chat window and do in diff. colors

// depending on whether I or someone else is sender

try {

if (Sender.equals(sUsername)) {

doc.insertString(doc.getLength(), sUid, uid);

} else {

doc.insertString(doc.getLength(), sUid, uidIn);

}

// detect url in message body

StringTokenizer st = new StringTokenizer(iMsg.getBody()," ",true);

while (st.hasMoreTokens()) {

String token = st.nextToken();

                if (token.startsWith("http://") || token.startsWith("https://")) {

linksAttr.addAttribute(HTML.Attribute.HREF, token);

doc.insertString(doc.getLength(), token, linksAttr);

// System.out.println(token);

} else if (token.indexOf("@") != -1 ) {

linksAttr.addAttribute(HTML.Attribute.HREF, “mailto:” + token);

doc.insertString(doc.getLength(), token, linksAttr);

} else if (token.startsWith(“www.”)) {

           linksAttr.addAttribute(HTML.Attribute.HREF, "http://" + token);               

doc.insertString(doc.getLength(), token, linksAttr);

} else {

if (Sender.equals(sUsername)) {

doc.insertString(doc.getLength(), token, msgBody);

} else {

doc.insertString(doc.getLength(), token, msgBodyIn);

}

}

// doc.insertString(doc.getLength(), " ", null);

}

doc.insertString(doc.getLength(), “\n”, null);

} catch(BadLocationException e) {

System.out.println("Exception Caught: " + e.getMessage() + " " + e);

}

}

textAreaTop.setCaretPosition(textAreaTop.getDocument().getLength());

}