• Post Reply Bookmark Topic Watch Topic
  • New Topic

components inside array..?  RSS feed

 
Nicky Eng
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 300 labels declared and initialised which is damn annoying with hard coded.

the labels are green color in background, so i want them to be red color if it get clicked by mouse.

the problem is i dont want to hard code it all in the mouseClicked() method....300 labels which is quite alot.

is it a easier way to do it or i just have to hard code it 1 by 1?
i was thinking of put all JLabel component into array, and use for loop to setBackground to red color;
like that i dont have to use type "setBackground" again and again to each label..

here is the code:
 
Nicky Eng
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i just checked on one website,


could this work??
i can't try it because im in cyber cafe. But i will try it when i home.
If anyone knows earlier, please let me know....
thank you.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
reasonably simple like this
(change 100 to 300, and modify gridlayout specs)



[EDIT] remove extends JFrame
[ October 04, 2006: Message edited by: Michael Dunn ]
 
Nicky Eng
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you.
i will try it later i home.

thanks again.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!