• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding a click method to all jlabels

 
Matt Klein
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21113
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need to extend JLabel to listen for mouse clicks.

How to Write a Mouse Listener

 
Darryl Burke
Bartender
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic