automatically detect hyperlinks
I am trying to display text stream I get from a socket on to a JTextPane. I would like to detect
hyperlinks (NOT with html tags ) and also if possible emoticons and display them accordingly.
It is not an html web page so JTextPane does not do that automatically. Is there an existing class
in java that can do that ?
TY
Re: automatically detect hyperlinks
I don't think so, but you might find a third party library that does that stuff. You're probably better off just writing it yourself.
Re: automatically detect hyperlinks
Quote:
to detect hyperlinks (NOT with html tags )
Can you show an example of a hyperlink without html tags?
Or show the text you would be trying to display and what part of it would be a hyperlink that you want to detect.
Re: automatically detect hyperlinks
I meant something like the editor this forum uses for post editing Eg. This link http://www.javaprogrammingforums.com...2605&noquote=1 and emotion :) are automatically converted. Even though I typed without the "url tag" and also text imoticon ": )"
Both the link and emoticon are converted automatically to a hyperlink and an image. I understand from Kevin's response there isn't such a class in Java so I might have to do it myself, unless it is available already.
Re: automatically detect hyperlinks
Quote:
Originally Posted by
dabdi
I meant something like the editor this forum uses for post editing Eg. This link
http://www.javaprogrammingforums.com...2605&noquote=1 and emotion :) are automatically converted. Even though I typed without the "url tag" and also text imoticon ": )"
Both the link and emoticon are converted automatically to a hyperlink and an image. I understand from Kevin's response there isn't such a class in Java so I might have to do it myself, unless it is available already.
Can't you just check if the String contains "http://" and then do whatever it is you want to do with it based on that?
Re: automatically detect hyperlinks
Are you defining a hyperlink as text of a URL? Begins with http: and goes to the end of the word.
As for emoticons, are they some special kind of markup known and used by the displayer of the text to insert small images in the display?
Re: automatically detect hyperlinks
Quote:
Originally Posted by
shia
Can't you just check if the String contains "http://" and then do whatever it is you want to do with it based on that?
Thanks. I forgot about regex. I found something that does the job for most links after modifying it a bit.
Code java:
private static final Pattern linkPattern = Pattern.compile(
".*(https?|ftp|file)(://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]).*",
Pattern.DOTALL);
System.out.println("<url>" + matcher.group(1) + matcher.group(2) + "</url>");
It detects them but JtextPane is not dispalying a hyperlink after I add the url tag for some reaons.
I think I can work it out from here.
Nothing I can do about the emoticons though.
Re: automatically detect hyperlinks
Quote:
Originally Posted by
dabdi
Thanks. I forgot about regex. I found something that does the job for most links after modifying it a bit.
Code java:
private static final Pattern linkPattern = Pattern.compile(
".*(https?|ftp|file)(://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]).*",
Pattern.DOTALL);
System.out.println("<url>" + matcher.group(1) + matcher.group(2) + "</url>");
It detects them but JtextPane is not dispalying a hyperlink after I add the url tag for some reaons.
I think I can work it out from here.
Nothing I can do about the emoticons though.
http://download.oracle.com/javase/1,...harSequence%29
Re: automatically detect hyperlinks
Try another class. Add this code to a GUI container:
Code :
// JEditorPane testing
JEditorPane jep = new JEditorPane();
jep.setContentType("text/html");
jep.setText("<H1>an H1 Header</H1><br><P>First para<p>Second para<p>And the Third"
+"<A href=\"http://Test.com\">A hyperlink</A>"
);
add(jep);
Re: automatically detect hyperlinks
Quote:
Originally Posted by
Norm
Try another class. Add this code to a GUI container:
Code :
// JEditorPane testing
JEditorPane jep = new JEditorPane();
jep.setContentType("text/html");
jep.setText("<H1>an H1 Header</H1><br><P>First para<p>Second para<p>And the Third"
+"<A href=\"http://Test.com\">A hyperlink</A>"
);
add(jep);
I know JEditorPane does that but I was under the impression JTextPane does it too since it is a subclass.
Anyway my text pane appends strings continually and colors the lines as needed. I have had difficulty to do that with an JEditorPane but for JTextPane I have this:
It is a debug window that can be used for chatting too hence why I needed the emoticons.
If I can append to a JEditorPane then I will use it.
TY
Re: automatically detect hyperlinks
I forgot toa add the test for matching im previous sample code. It is detecting them fine
but JTextPane is not displaying a hyperlink. It seems that I have to use a JEditorPane
Code java:
Matcher matcher = linkPattern.matcher(line);
if(matcher.matches()) {
printDebug("<url>" + matcher.group(1) + matcher.group(2) + "</url>");
}
Re: automatically detect hyperlinks
Re: automatically detect hyperlinks
Quote:
Originally Posted by
KevinWorkman
The problem is that JTextPane & JEditorPane are difficult to use for editable html data. SetPage(), URL etc. are all for one time updates. So far I was able to replace the whole document every time an html link and an emoticon pops up but that is very slow. To update html data I had to use some kind of HTMLEditorKit. And also add mouse listeners and other stuff. Not a satisfactory solution overall. Actually implementing emoticons turn out to be easier, all I had to do was replace them with icons from this forum and it worked fine.