[java] self writing java code

Started by
3 comments, last by cwl157 15 years, 5 months ago
So i have a java GUI with a bunch of labels. I am using the netbeans gui creator to create it so i can not have an array of JLabels so i have to create them 1 at a time. I want to loop through them and set the text of each one seperately. If all the JLabel instances are jLabel1, jLabel2, jLabel3 etc, is there a way to have java create this instance name and call the setText() method for that instance. I thought about taking the word "jLabel" as a string and i can concatenate i from the loop to the end to product jLabel1, jLabel2, etc but then how do i make this string call the method of jLabel1 or jLabel2 or whatever it is. Is this possible? Thanks.
Advertisement
Yep, you can envoke methods by their String name using Java Reflection. Check it out and let us know if you continue to have trouble.
ok so i have some questions like the

Object o = m.invoke(t, new Locale(args[1], args[2], args[3]));


it says the first parameter is the instance of the object that the method will be invoked from. And it declares t as a new instance of a class. However, i already have the instances i just want to invoke a method on each of them but the instance names increase by 1 each time, so can i pass in a string where t is, that string being "jLabel1" and then have it take it as an instance of JLabel named jLabel1? I am confused about how i apply this to what i want cause this is about getting methods from a class and creating new instances.
Quote:Original post by cwl157
So i have a java GUI with a bunch of labels. I am using the netbeans gui creator to create it so i can not have an array of JLabels so i have to create them 1 at a time.

That's why I prefer doing my GUIs in code. It's really not that hard and it gives you so much more control...
no its impossible for me to do that. Maybe now that i have used the GUI builder a little i could understand it more, but i tried all that once and spent about 2 days lost in all the container classes and what not.

This topic is closed to new replies.

Advertisement