Link to home
Start Free TrialLog in
Avatar of JAVAnewbie
JAVAnewbie

asked on

Hyperlink on JLabel?

hi, is there a way to make my label to become a hyperlink to certain websites?
like when i click on the label, it will direct me to the website?

Avatar of zzynx
zzynx
Flag of Belgium image

yourLabel.setText("www.google.com");
yourLabel.addActionListener( new ActionListener() {
   public void actionPerformed(ActionEvent evt) {
        Runtime.getRuntime().exec("cmd /c start http://www.google.com");
   }
});
Easier to use a button, and it will provide better use feedback.

if you want it to have a link underneith
You can use
yourLabel.setText("<HTML><U>www.google.com</U></HTML>")

and also use font

You can also chnage Cursor on mouse over event to Hand to give it a perfect look


Avatar of JAVAnewbie
JAVAnewbie

ASKER

zzynx >> i am not sure if JLabel can have ActionListener assigned to it?
ASKER CERTIFIED SOLUTION
Avatar of armoghan
armoghan
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
armoghan >> let me try first.
>> i am not sure if JLabel can have ActionListener assigned to it?

It can't
>> zzynx >> i am not sure if JLabel can have ActionListener assigned to it?
You are right of course.
Stupid me ;°)

You'll have to go for

yourLabel.setText("www.google.com");
yourLabel.addMouseListener( new MouseAdapter() {
   public void mouseClicked(MouseEvent e)
        Runtime.getRuntime().exec("cmd /c start http://www.google.com");
   }
});

as armoghan said.
tks everyone