| Author |
Adding a click method to all jlabels
|
Matt Klein
Greenhorn
Joined: May 17, 2011
Posts: 2
|
|
Dear all
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.
Until recently the only programming I have done is javascript - I am finding the transition a little hard !!
Many thanks in advance
Matt
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
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.
|
 |
Matt Klein
Greenhorn
Joined: May 17, 2011
Posts: 2
|
|
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 !)
Cheers
Matt
|
 |
Darryl Burke
Bartender
Joined: May 03, 2008
Posts: 4164
|
|
You don't need to extend JLabel to listen for mouse clicks.
How to Write a Mouse Listener
|
luck, db
There are no new questions, but there may be new answers.
|
 |
Darryl Burke
Bartender
Joined: May 03, 2008
Posts: 4164
|
|
Oh, and we would like that you BeForthrightWhenCrossPostingToOtherSites.
http://www.java-forums.org/new-java/44207-add-click-method-all-jlabels.html
edit: http://www.daniweb.com/software-development/java/threads/364750
|
 |
 |
|
|
subject: Adding a click method to all jlabels
|
|
|