I am making a program which adds lots of jlabels to a frame (the idea, eventually, is that each jlabel will link to a different program on my dekstop)
The trimmed down code below shows that I have a script which creates an array of multiple jlabels which are appended to the jframe (I eventually got this working). I am now a little stuck and I would really like to know how to add the same method to each jlabel so that, when clicked, a pop up label tells me its position in the array. (e.g. the 7th element)
One idea I have had is to use the "extends" keyword. I believe this is a form of inheritance, so that all my jlabels have a method to respond to a click event. I am not sure how to go about this. I know how to add a click event method for a single label created manually, but not for multiple labels created programatically.
It would certainly be possible to extend JLabel so that it would respond to clicks. Using the word "extends" would just be the first step; you would have to write code which listened for clicks and did something when they were detected.
But I wouldn't do that. I would just use JButton instead of JLabel; it looks similar and the "on click" behaviour is already built in.
Joined: May 17, 2011
Hello there Paul
Thanks for the button suggestion - it certainly would make more sense to use this if the click functionality is already present !
However, for the purposes of learning a new language I would also like to do this using the label.
So I will try both ideas. Is it ok if I post back here when it inevitably doesn't work? ( I am not too confident !)