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
> 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.
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.