Hi, I need to use JLabel as hyperlink. i have array of JLabels used as hyperlink. I am trying to use mouse click event on these JLabels so that hyperlink opens some window. But the problem is mouse click event what i used is not working here. Can mouse click event be used for arrays. Let me know.
Seems a rather unusual design, clicking on a label; one would usually associate a button with the URLs and click on the button.
You ought however be able to add a MouseListener to a JLabel and open the URL from its mouseClicked method. If you have an array of URLs then it is probably easy to use a for-each loop to iterate through the array setting up the Listeners and URLs together.
Originally posted by Seema Sharma: Hi, I need to use JLabel as hyperlink. i have array of JLabels used as hyperlink. I am trying to use mouse click event on these JLabels so that hyperlink opens some window. But the problem is mouse click event what i used is not working here. Can mouse click event be used for arrays. Let me know.
You can have one class which extends JLabel. Let us call it MyLabel. In the MyLabel constructor you can define data such as display text, the URL to open. You also add a mouse listener, and the code to open the URL when the mouse click happens.
Now in your frame, you create an array of MyLabel instead of JLabel. That way each instance of MyLabel will know what to do when it gets the mouse click notification.
Originally posted by Seema Sharma: I need to use JLabel as hyperlink.
Like Campbell rightly pointed out, JLabel is a strange kind of control. You can make the JButton look and behave like a hyperlink by making use of: 1) JButton supports html as display text. So you can definitely make it display something with a hyperlink look by simply using the <u></u> tag 2) Check out JButton's setContentAreaFilled(boolean isFilled) method 3) Check out JButton's setBorderPainted() method.
Hi All, I have implemented mouse click event for JLabel array. Thanks for all your help. First time its working fine. Next time when this code is called on clicking 'OK' button of other dialog box, it does not work.
In the above code though both e.getComponent() and labelN[nIndex] are containing same values, still "if" condition fails when code is called from other pop-up. Can you suggest me what could have gone wrong?