how to new with dynamic name

Hi

I am pretty sure this is simple, but not sure how to ask the question in the search engines.

so I will ask it here.

I have a enum class like so

staticprotectedenum Panels{

TRANSACTIONS(0,"Transcations","View Transactions"),

OWNERS(1,"Owners","View all the Owners"),

PORTFOLIO(2,"Portfolio","View all the Portfolios"),

EQUITIES(3,"Equity","View all the Equities"),

COMPANIES(4,"Company","View all the Companies"),

REPORTS(5,"Report","View all the Reports"),

MAINTENANCE(6,"Maintenance","Do Maintenance on the Database"),

ADMIN(7,"Admin","Do Admin on the Database");

// Constants

publicfinalstaticint MAXPANELS = 8;

// Attributes

/**

* Order of the panel

*/

privatefinal Integer panelOrder;

/**

* Name of the Panel

*/

privatefinal String panelName;

/**

* Tool Tip of the panel

*/

privatefinal String panelTip;

// Constructor

Panels(Integer panelOrder, String panelName, String panelTip){

this.panelOrder = panelOrder;

this.panelName = panelName;

this.panelTip = panelTip;

}

// Methods

public Integer panelOrder(){

return this.panelOrder();

};

public String panelName(){

return this.panelName;

}

public String panelTip(){

return this.panelTip;

}

}

I want to be able to add a attribute to the class to all for a name of a class so that i can do

Panel[] panels =new Panel[NUMPANELS];

// Create each panel, with GridBagLayout

for (Panels panel : Panels.values()){

panels[panel.panelOrder] =new Equities(parent,

new GridBagLayout());

jTabbedPane.addTab(panel.panelName,

null,

panels[panel.panelOrder],

panel.panelTip());

}

but instead of new Equities, I would like to have it call new panel.getclassname().

Thanks

[3725 byte] By [KeyzerSuzea] at [2007-11-27 7:55:09]
# 1
as is typical with life, I have come across static Class Class.forName(String name)which i believe is the answer to my questions, but how do i pass parameters to the initializing classany pointers to so examples
KeyzerSuzea at 2007-7-12 19:36:28 > top of Java-index,Java Essentials,Java Programming...
# 2
I don't understand what it is you are trying to do (hopefully someone else does). Could you precisely describe exactly what it is you want to do?
petes1234a at 2007-7-12 19:36:28 > top of Java-index,Java Essentials,Java Programming...
# 3

I want to add a field to the panels class that is a string of the name of a class that i want to be able todo a new against.

enum panel{

panel1(ThisIsClass1),

panel2(ThisIsClass2);

private String nameofclass;

panel( String classname){

this.nameofclass = classname;

}

public classname(){

return classname;

}

}

and then be able to do

for ( panel p; panel.getvalues()){

new p.classname (some parameters);

}

KeyzerSuzea at 2007-7-12 19:36:28 > top of Java-index,Java Essentials,Java Programming...
# 4

> I want to add a field to the panels class that is a

> string of the name of a class that i want to be able

> todo a new against.

>

I'm still not sure, and again, it could be me. From what I understand, you really shouldn't, try to initialize a class from a string. It smells of bad design. It may not even be possible to do.

Also, are you trying to add dynamic info to an enum? Again from what I understand (and believe me, I could be wrong), I don't think enums work that way. Perhaps you could have a separate class that holds the enum value and a reference to the class you wish to enum and create an array of objects of this class.

Again, my answer is vague because my understanding of your question is vague. Hopefully someone else will understand all this better. Good luck.

petes1234a at 2007-7-12 19:36:28 > top of Java-index,Java Essentials,Java Programming...
# 5
Also, why are you modifying your enum class as "static protected"? This doesn't make sense to me and may not be "legal".
petes1234a at 2007-7-12 19:36:28 > top of Java-index,Java Essentials,Java Programming...
# 6
i suppose another way of putting it is, I want to have a list of classes - defined at compile time that I can cycle through in a for loop, that I can instantiate.pseudo code for x in ( classA, ClassB, ClassC) new x(Arg)done
KeyzerSuzea at 2007-7-12 19:36:28 > top of Java-index,Java Essentials,Java Programming...
# 7
i have found this http://www.javageeks.com/Papers/ClassForName/ClassForName.pdfseems to be along the lines that I am after
KeyzerSuzea at 2007-7-12 19:36:28 > top of Java-index,Java Essentials,Java Programming...
# 8

> i suppose another way of putting it is, I want to

> have a list of classes - defined at compile time that

> I can cycle through in a for loop, that I can

> instantiate.

>

>

> pseudo code

>

> for x in ( classA, ClassB, ClassC)

>

>new x(Arg)

>

> done

My guess is that you want a list of references to objects of (different?) classes that you can cycle through. That should be easy enough to do with an array or arraylist of the most primitive class in the hierarchy. Sometimes even better is to have an array or arraylist of interface that is implemented by all of the classes.

petes1234a at 2007-7-12 19:36:28 > top of Java-index,Java Essentials,Java Programming...
# 9

found what I was after here

http://java.sun.com/developer/technicalArticles/ALT/Reflection/

the section on constructors.

problems with the list of objects, is they have to instantiated when the list is build. I just want a list of class that could be instantiated, but is determined at run time.

KeyzerSuzea at 2007-7-12 19:36:28 > top of Java-index,Java Essentials,Java Programming...
# 10
You could use reflection, but since you have a finite list of classes that's known in advance, it might be better to just use a factory method.
DrClapa at 2007-7-12 19:36:28 > top of Java-index,Java Essentials,Java Programming...
# 11

Yeah I thought of that,

some sort of big switch statement. It was also a learning exercise

ended up with some thing like this

// Define the call type for the constructor

final Class partypes[] = new Class[2];

partypes[0] = MainMenu.class;

partypes[1] = LayoutManager.class;

// The constructor

Constructor ct;

// Create argument list

final Object arglist[] = new Object[2];

arglist[0] = parent;

Integer panelIdx = 0;

Panel[] panels = new Panel[NUMPANELS];

// Create each panel, with GridBagLayout

for (Panels panel : Panels.values()) {

// No class do nothing

if (panel.className == null) {

continue;

}

try {

// Find the constructor for the class

ct = panel.className().getConstructor(partypes);

// Create a new GridBaglayout for each panel

arglist[1] = new GridBagLayout();

// Call the constructor

panels[panelIdx] = (Panel) ct.newInstance(arglist);

jTabbedPane.addTab(panel.panelName,

null,

panels[panelIdx],

panel.panelTip());

panelIdx++;

} catch (Throwable e) {

System.err.println(e);

e.printStackTrace();

System.exit(0);

}

}

return panels;

}

the only advantage is I can just update my enum list and create the classes with out having to update the factory.

KeyzerSuzea at 2007-7-12 19:36:28 > top of Java-index,Java Essentials,Java Programming...