Click to See Complete Forum and Search --> : naming Swing objects
Shaique
November 8th, 2001, 01:57 PM
Hi
I was trying to dynamically name a swing object and could'nt do it , for some reason the constructor kept taking the argument as a String object and it did not call the toString like how System.out.println would call.
SAMPLE CODE++++++checkboxes is a string vector
for (int i=1;i==checkboxes.size();i++)
{
temp_obj = checkboxes.elementAt(i);
//JCheckBox temp.toString() = new JCheckBox(temp.toString());
String tempname = temp_obj.toString();
toppanel.add(tempname.toString());
}
Please help !
poochi
November 8th, 2001, 02:57 PM
I dont understand the question. Can you please elaborate ??? What do you mean by "dynamically
name a "swing object" "?? I dont understand what you are doing in your sample code.
toString method will return string representation of an Object. You can not give a "name" to
object reference(variable) at runtime.
Captain Nuss
November 8th, 2001, 03:16 PM
What you want to do isn't possible. You must name an object at the time of writing, you can't just assign it a name at runtime.
----------------
You can contact me directly at Christoph.Schulze@gmx.co.uk
Don't forget your parsley cause you can't eat your dog after having stolen him from some animal shelter and having drowned him in the Atlantic Ocean.
Shaique
November 8th, 2001, 03:20 PM
Ok , Thanks
Is there anyway to beat this problem ? i need to fill a panel with checkboxes based on filenames present in a particular directory. i could get till the getting of the names of the files into a String array but i cane create checkboxes with them.
If u know any way out.
dlorde
November 9th, 2001, 05:43 AM
Can you explain exactly what you're trying to achieve?
You code doesn't do much except add the Strings in checkboxes to toppanel in a very convoluted way (or it would if the loop was correct). This does the same thing:for(int i=0; i < checkboxes.size(); i++) {
toppanel.add((String)checkboxes.elementAt(i));
}
Dave
dlorde
November 9th, 2001, 05:49 AM
OIC! What about:for(int i=0; i < checkboxes.size(); i++) {
toppanel.add(new JCheckBox((String)checkboxes.elementAt(i)));
}
Dave
Shaique
November 9th, 2001, 09:23 AM
The idea was to name an object dynamicaly !
The name of the object is coming from a Vector of String objects.:-)
Its a peculiar requirement.
Shaique
November 9th, 2001, 09:27 AM
Yeah, thats what i was trying to do .
well if another requierment is to be able to create objects of classes by by class name from a String datatype.
poochi
November 9th, 2001, 09:47 AM
> to create objects of classes by by class name from a String datatype.
Well. this is easy.. you should work with java.lang.Class class.
Object tMyObject = Class.forName("myclassname").newInstance();
Norm
November 9th, 2001, 06:27 PM
It appears that you are trying do create a string that is a "Java source" statement, and then compile it and execute it inline. In other languages such as perl, javascript or REXX that is done with the evalutate() command.
For example:
If you wanted to build and execute an assignment statement such as:
XXX = YYY + 2;
You could code:
String x = "XXX";
String y = "YYY"
and then do: evaluate(x + " = " + y + " + 2;");
The String in the ()'s would evaluate to the statement: XXX = YYY + 2; which would then be executed.
Nothing like that in Java. Reflection comes the closest.
Norm
Shaique
November 10th, 2001, 01:10 PM
Yes,
Thats precisely what i wanted to do, unfortunately ...... your right ! :-)
However i did get around making objects out of string name equivalents of existing classes.
Class.forName("RUN-TIME-STRING").newInstance()
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.