1,105,578 Community Members

Add a click method to all JLabels

Member Avatar
mklein
Newbie Poster
11 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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 !!

javax.swing.JLabel[] labels = new javax.swing.JLabel[line_count]; 

  for (int i = 0; i < mystrings.length; i++) { 
    labels[i] = new javax.swing.JLabel("hello");  //fill the array with the jlabels
       cp.add(labels[i]);   //append the labels to the frame container
   pack();
}

Many thanks in advance

Matt

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
1
 
Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
0
 

From that other answer

You could use the ClientProperty associated with each JComponent to save any info about a component you want, like its position in an array. See the put... and get... methods.

Adding to that a bit: you can create a single listener instance that uses the getSource() method to get the label that was clicked, cast it to JLabel and get the value of that property. Add the listener to each label as you create it in your loop.

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,751 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
1
 

Create a small class to handle the click. Have a constructor for that class that takes an int as parameter and saves its value. Crate a new instance for each JLabel, with the JLabel's index as parameter. Now when you handle the event you have the index of the JLabel it relates to.
ps I also like Norm's client property approach
pps Forum protocol is that, if you cross-post in different sites, you should say so.

Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
0
 

I was also going to suggest something similar to what James mentioned above originally, but I like that the ClientProperty approach would allow for a single listener for all labels and the property is easier to change at run time if the array position were to change.

You could swap a custom listener object with a new one if necessary, but changing the client property seems easier and cleaner than digging out the listener and replacing it.

Member Avatar
mklein
Newbie Poster
11 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello everyone

Sorry, I hold my hands up at posting on another site. I am a teacher and I only get a very limited amount of time I can spend on this hobby. I should have read the site rules more closely.

Anyway, thank you all for the replies. I think James' suggestion is probably closest to what I had in mind - though I will try all of your suggestions and I can hopefully get one of them to work !

I will have a go at coding it this evening. Is it ok if I post back here when it inevitably doesn't work? (not very confident !)

Member Avatar
Darryl.Burke
Newbie Poster
11 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
1
 
Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,751 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

I think James' suggestion is probably closest to what I had in mind - though I will try all of your suggestions and I can hopefully get one of them to work !

I should point out that I just posted that for its conceptual simplicity assuming someone with a non-OO background. Personally I'm a long-time fan and user of client properties.
As a rule of thumb, if the logic is in with the GUI then you may probably want the array index, so that's fine. If you're doing the full MVC thing then you probably want the corresponding object in the model, for which a client property can give you the direct reference immediately.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: