JLabel's Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nanhiPari
    New Member
    • Jul 2007
    • 8

    #1

    JLabel's Problem

    hello everyone

    i need to create DYNAMIC JLABElS(say 5)..yes i know it sounds easy

    but i also have to give them Events n then want to refer each of them when ever Mouse is Clicked on a Specific Label with its Label Name or Text..

    i tried it by...



    Code:
    Object lbl_name
    
    for(n1=1;n1<=5;n1++)
     {
                                 
            lbl_name = new String("lbl_cal");
                                 
    
             lbl_name = new JLabel();
                                                          
             ((JLabel)lbl_name).setName(lbl_name.toString());
                                 
              panel1.add((JLabel) lbl_name, new org.netbeans.lib.awtextra.AbsoluteConstraints(x, y, 40, 20));
                     
                                 
               ((JLabel)lbl_name).addMouseListener(this);
                                 
                ((JLabel)lbl_name).setText(n1);
    
    x = x+10;
    y = y+2;
    }



    n then calling it in MouseClicked event..



    Code:
    public void mouseClicked(MouseEvent me)
      {
        
        System.out.println("CLICKED on Label..." + ((JLabel)lbl_name).getText());
        
      }
    but this is not giving me the REsult..as this alwayz prints 5 (i.e LAST JLABELS setText..


    PLZ help!!!need Urgently..

    thankz is Advance

    Cheerz!!
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by nanhiPari
    hello everyone

    i need to create DYNAMIC JLABElS(say 5)..yes i know it sounds easy

    but i also have to give them Events n then want to refer each of them when ever Mouse is Clicked on a Specific Label with its Label Name or Text..
    Users prefer to click on JButtons; not on JLabels. You could do it but it'll confuse
    your users.

    kind regards,

    Jos

    Comment

    • nanhiPari
      New Member
      • Jul 2007
      • 8

      #3
      Originally posted by JosAH
      Users prefer to click on JButtons; not on JLabels. You could do it but it'll confuse
      your users.

      kind regards,

      Jos
      well yesh JOs i know that user click on the JButtons. but according to the thing i need to do is requiring to click on the JLabels


      thankz for ur Reply

      any ANswerz to my Problem ??

      cheerz!!
      thankz in advance

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by nanhiPari
        well yesh JOs i know that user click on the JButtons. but according to the thing i need to do is requiring to click on the JLabels
        You're definitely on the wrong track then because a JLabel doesn't respond to
        events and using low level MouseEvents for that will be a total mess because
        you have to poop up that JLabel to an 'almost' button.

        Better remove the JButton border so it'll look like a JLabel. See the
        setBorderPainte d(boolean painted) method.

        kind regards,

        Jos

        Comment

        Working...