Results 1 to 4 of 4
- 05-17-2011, 10:08 PM #1
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
Add a click method to all jlabels
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 thanksJava Code: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();}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(); }
Matt
- 05-17-2011, 10:19 PM #2
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.add the same method to each jlabel so that, when clicked, a pop up label tells me its position in the array
Cross posted at:
http://www.daniweb.com/software-deve...threads/364750Last edited by Norm; 05-17-2011 at 10:23 PM.
- 05-18-2011, 07:23 AM #3
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
Hi Norm
Thank you for this suggestion. I will give it a try. As I create the labels (in the for loop), I guess I could set the ClientProperty then.
Cheers
Matt
- 05-18-2011, 08:03 AM #4
Similar Threads
-
Need help with JPanels and JLabels
By littledrummerboy in forum New To JavaReplies: 2Last Post: 05-13-2011, 10:43 PM -
Multiline JLabels
By davetheant in forum New To JavaReplies: 3Last Post: 01-21-2011, 01:18 PM -
can i have a JList of Jlabels?
By chayan in forum AWT / SwingReplies: 3Last Post: 07-04-2010, 12:19 AM -
Overlapping JLabels
By techbossmb in forum AWT / SwingReplies: 3Last Post: 09-21-2009, 03:21 PM -
problem with JLabels
By geork in forum New To JavaReplies: 3Last Post: 01-31-2008, 02:30 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks