Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

changing instance names  RSS feed

 
Ronan Cantona
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a form created where i click on a button and new labels are added to it. The only thing is i only can get it adding the labels under the same instance name. e.g

JLabel jLabel2 = new JLabel(image);
addComponent(cp, jLabel2,373+x,47+x,70+x,18+x);

its always jLabel2 now i was wondering is there a way to change the instance name to be something different everytime the button is pressed
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, it's important to notice that instances don't have names, only variables have. And variables are "only" references to instances, which means that there can be (and typically are) instances that are referenced by more than one variable, and even instances that are referenced by none.

Now, if you want to hold references to an unknown number of instances, you should take a look at the collection API: http://java.sun.com/docs/books/tutorial/collections/index.html

You will most probably want to use a java.util.ArrayList.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But in this case do you REALLY need to store all those JLabel references? Labels are not typically interactive and so there usually is not a reason to store their references. This might be different if you had multiple JTextFields or some other interactive control that you might want to manipulate.

Layne
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Layne Lund:
But in this case do you REALLY need to store all those JLabel references? Labels are not typically interactive and so there usually is not a reason to store their references. This might be different if you had multiple JTextFields or some other interactive control that you might want to manipulate.


Good point...
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ronan Cantona:
I have a form created where i click on a button and new labels are added to it. The only thing is i only can get it adding the labels under the same instance name. e.g

JLabel jLabel2 = new JLabel(image);
addComponent(cp, jLabel2,373+x,47+x,70+x,18+x);

its always jLabel2 now i was wondering is there a way to change the instance name to be something different everytime the button is pressed


What do you mean by "instance name"? Are you asking if there's a way for it to be called something other than "jLabel2" or what?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!