Java Program: This program creates a JLabel with a HyperLink. If you click on this link, a browser will open with the adress embeded.
[sourcecode language='java']
package javaapplicationmodels;
import java.io.IOException;
import javax.swing.*;
import java.awt.*;
public class JLabelLink extends JFrame {
private static final long serialVersionUID = 1L;
public JLabelLink() {
super() ;
setTitle( “Click on the JLabel to start the webpage.” ) ;
setDefaultCloseOperation( EXIT_ON_CLOSE ) ;
JPanel pane = new JPanel();
setContentPane(pane);
setLayout(new FlowLayout());
JLabel label= new JLabel( “Visit iWebie” , JLabel.CENTER );
label.setBackground(Color.RED);
getContentPane().add(label) ;
label.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
if(evt.getClickCount() > 0){
try {
Process pc = Runtime.getRuntime().exec(”cmd.exe /c start http://www.iwebie.com”);
} catch (IOException ex) {
System.out.println(ex.getMessage());
System.out.println();
}
}
}
});
setSize( 400 , 100 );
setVisible( true );
}
public static void main(String[] args) {
new JLabelLink () ;
}
}
[/sourcecode]











It does not work in Linux!
This is really lame.
very good example…thanxxxxxxxxx
This may not be revelant but i figured i’d post this anyway. If you’re using ubuntu 8.10 you may be in for some issues with the network manager. For some unknown reason it stops functioning. You will need to manually set you’re resolv.conf with your ISP’s DNS servers. That file is located in /etc/network/resolv.conf
thank u 4 ur code………its a good example for creating hyper links…….its help ful 2 my project…..
It does not only “not work in Linux”. In fact, it works on Windows exclusively.
Please do not spread this bad code. Replace it with a proper solution or remove it or – at the very least- inform people that this is nothing but a workaround for confirmed windows-only environments.
Leave a Reply