PDA

Click to See Complete Forum and Search --> : [RESOLVED] Clickable link to get the browser in a JLable


eranga262154
Mar 26th, 2008, 05:25 AM
Hi all,

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


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.

ComputerJy
Mar 26th, 2008, 09:13 AM
:)
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();
}
}

});
}
}

eranga262154
Mar 26th, 2008, 09:02 PM
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.

ComputerJy
Mar 27th, 2008, 01:53 AM
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).

eranga262154
Apr 8th, 2008, 10:31 PM
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.

ComputerJy
Apr 8th, 2008, 10:51 PM
My code is compatible with all OSs

eranga262154
Apr 8th, 2008, 11:02 PM
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 :)

ComputerJy
Apr 8th, 2008, 11:32 PM
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