Forums Register Login

changing instance names

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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...
+Pie Number of slices to send: Send
 

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?
Look! I laid an egg! Why does it smell like that? Tiny ad, does this smell weird to you?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 404 times.
Similar Threads
Accessing a jLabel with % reference?
Drawing with mouse
Too many ActionEvents after button press.
JCheckBox?
Change jLabel properties in loop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:20:54.