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

Recommended Answers

All 7 Replies

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.

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.

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.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.