Results 1 to 4 of 4
  1. #1
    mklein is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

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

    Java 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();
    }
    Many thanks

    Matt

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    add the same method to each jlabel so that, when clicked, a pop up label tells me its position in the array
    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.

    Cross posted at:
    http://www.daniweb.com/software-deve...threads/364750
    Last edited by Norm; 05-17-2011 at 10:23 PM.

  3. #3
    mklein is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default

    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

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

Similar Threads

  1. Need help with JPanels and JLabels
    By littledrummerboy in forum New To Java
    Replies: 2
    Last Post: 05-13-2011, 10:43 PM
  2. Multiline JLabels
    By davetheant in forum New To Java
    Replies: 3
    Last Post: 01-21-2011, 01:18 PM
  3. can i have a JList of Jlabels?
    By chayan in forum AWT / Swing
    Replies: 3
    Last Post: 07-04-2010, 12:19 AM
  4. Overlapping JLabels
    By techbossmb in forum AWT / Swing
    Replies: 3
    Last Post: 09-21-2009, 03:21 PM
  5. problem with JLabels
    By geork in forum New To Java
    Replies: 3
    Last Post: 01-31-2008, 02:30 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •