Results 1 to 8 of 8

Thread: [RESOLVED] Clickable link to get the browser in a JLable

  1. #1

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Resolved [RESOLVED] Clickable link to get the browser in a JLable

    Hi all,

    I want to add a html link to a web page on a JLabel. I try something this.

    Code:
            String htmtString = "<html><head></head><body><a href = \"http://www.google.com/\">Google</a></body></html>";
            
            jLabel1.setText(htmtString);
    Actually it display the blue color link on the label. But I can't click and go the site. How can I do it.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Clickable link to get the browser in a JLable


    Code:
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.io.IOException;
    
    import javax.swing.JLabel;
    
    public class Util {
    	public static void main(String[] args) {
    		JLabel lbl = new JLabel();
    		lbl.addMouseListener(new MouseAdapter() {
    
    			@Override
    			public void mousePressed(MouseEvent e) {
    				try {
    					Runtime.getRuntime().exec("http://www.java.sun.com");
    				} catch (IOException e1) {
    					e1.printStackTrace();
    				}
    			}
    
    		});
    	}
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Clickable link to get the browser in a JLable

    Thanks for the replay.

    Actually I got a runtime exception,
    java.io.IOException: Cannot run program "http://www.java.sun.com": CreateProcess error=2, The system cannot find the file specified
    Do you have any idea why it is happened.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Clickable link to get the browser in a JLable

    If you're using windows then the default browser should start and navigate to java.sun.com anyway.

    I've attached a class called BrowserLauncher. This class is ready to use, it starts the default browser for Windows, Mac & Linux (Unless you're using FluxBox).
    Attached Files Attached Files
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Clickable link to get the browser in a JLable

    Thanks a lot pal. At the time I just write a code only working for Windows. Working with dlls. I'll look at your code and try to make it compatible with all OS such as Linux, Mac and so.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  6. #6
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Clickable link to get the browser in a JLable

    My code is compatible with all OSs
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  7. #7

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: [RESOLVED] Clickable link to get the browser in a JLable

    Yes pal, it is. What I say is, I want to look at my code with your one and check how about my attempt. Your code exactly same what I want
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  8. #8
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: [RESOLVED] Clickable link to get the browser in a JLable

    Quote Originally Posted by eranga262154
    Yes pal, it is. What I say is, I want to look at my code with your one and check how about my attempt. Your code exactly same what I want
    lol
    sorry for the misunderstanding then
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width