Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: automatically detect hyperlinks

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default 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


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: automatically detect hyperlinks

    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.

  4. #4
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default 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.

  5. #5
    Junior Member shia's Avatar
    Join Date
    Sep 2011
    Location
    Manchester, UK
    Posts
    19
    My Mood
    Nerdy
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: automatically detect hyperlinks

    Quote Originally Posted by dabdi View Post
    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?

  6. The Following User Says Thank You to shia For This Useful Post:

    dabdi (September 12th, 2011)

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?

  8. #7
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default Re: automatically detect hyperlinks

    Quote Originally Posted by shia View Post
    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.

    	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.

  9. #8
    Junior Member shia's Avatar
    Join Date
    Sep 2011
    Location
    Manchester, UK
    Posts
    19
    My Mood
    Nerdy
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: automatically detect hyperlinks

    Quote Originally Posted by dabdi View Post
    Thanks. I forgot about regex. I found something that does the job for most links after modifying it a bit.

    	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
    Last edited by shia; September 12th, 2011 at 02:17 PM. Reason: typo

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: automatically detect hyperlinks

    Try another class. Add this code to a GUI container:
            // 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);

  11. #10
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default Re: automatically detect hyperlinks

    Quote Originally Posted by Norm View Post
    Try another class. Add this code to a GUI container:
            // 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:
    void printDebug(final String str,final int id) {
    		EventQueue.invokeLater(
    				new Runnable() {
    					public void run() {
    						StyleContext sc = StyleContext.getDefaultStyleContext();
    					    AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY,
    					        StyleConstants.Foreground, eColors[id]);
     
    						int len = myDebug.getDocument().getLength();
    						myDebug.setCaretPosition(len);
    						myDebug.setCharacterAttributes(aset, false);
    						myDebug.replaceSelection(str + "\n");
    					}
    				}
    		);
    	}
    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

  12. #11
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default 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
    Matcher matcher = linkPattern.matcher(line); 
    			if(matcher.matches()) {
    printDebug("<url>" + matcher.group(1) + matcher.group(2) + "</url>");
     
    }

  13. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  14. #13
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default Re: automatically detect hyperlinks

    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.

Similar Threads

  1. [SOLVED] Detect USB signal
    By mine0926 in forum Java Native Interface
    Replies: 2
    Last Post: February 4th, 2011, 07:46 PM
  2. hyperlinks
    By shakoor in forum Java Servlet
    Replies: 1
    Last Post: December 21st, 2010, 10:17 AM
  3. Problem with automatically creating new objects with unique names
    By oniamien in forum Java Theory & Questions
    Replies: 2
    Last Post: December 4th, 2010, 02:38 PM
  4. [SOLVED] Why is my float automatically rounding and how do i get it to stop
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 18
    Last Post: August 26th, 2010, 01:41 PM
  5. Best way to automatically login user from a customer's web site.
    By ess_stegra in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: February 2nd, 2010, 12:16 PM