By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,740 Members | 844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,740 IT Pros & Developers. It's quick & easy.

JLabel's Problem

nanhiPari
P: 8
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...



Expand|Select|Wrap|Line Numbers
  1. Object lbl_name
  2.  
  3. for(n1=1;n1<=5;n1++)
  4.  {
  5.  
  6.         lbl_name = new String("lbl_cal");
  7.  
  8.  
  9.          lbl_name = new JLabel();
  10.  
  11.          ((JLabel)lbl_name).setName(lbl_name.toString());
  12.  
  13.           panel1.add((JLabel) lbl_name, new org.netbeans.lib.awtextra.AbsoluteConstraints(x, y, 40, 20));
  14.  
  15.  
  16.            ((JLabel)lbl_name).addMouseListener(this);
  17.  
  18.             ((JLabel)lbl_name).setText(n1);
  19.  
  20. x = x+10;
  21. y = y+2;
  22. }
  23.  
  24.  
  25.  



n then calling it in MouseClicked event..



Expand|Select|Wrap|Line Numbers
  1. public void mouseClicked(MouseEvent me)
  2.   {
  3.  
  4.     System.out.println("CLICKED on Label..." + ((JLabel)lbl_name).getText());
  5.  
  6.   }
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!!
Sep 7 '07 #1
Share this Question
Share on Google+
3 Replies


Expert 10K+
P: 11,448
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
Sep 7 '07 #2

nanhiPari
P: 8
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
Sep 9 '07 #3

Expert 10K+
P: 11,448
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
setBorderPainted(boolean painted) method.

kind regards,

Jos
Sep 9 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.