blarouche
asked on
Creating a new object from a list choice
Hi Experts
Here is my question :
From a list I get one or more selection that goes into a sortedList.
From that list I have to create a new object that correspond to the class name.
for example my first choice is SwingTrader and that choice correspond to the creation of a SwingTrader object...
Here is the code :
private class OKListener implements ActionListener {
public void actionPerformed(ActionEven t e) {
xxxxxxxx st = new xxxxxx(destListModel,point er);
} else{
}
What do I have to do to create that object automatically
Thank you
Here is my question :
From a list I get one or more selection that goes into a sortedList.
From that list I have to create a new object that correspond to the class name.
for example my first choice is SwingTrader and that choice correspond to the creation of a SwingTrader object...
Here is the code :
private class OKListener implements ActionListener {
public void actionPerformed(ActionEven
xxxxxxxx st = new xxxxxx(destListModel,point
} else{
}
What do I have to do to create that object automatically
Thank you
ASKER
I don't know how to figure that out
The next choice for example could be "swing"
When the user click on the OK button now I have to create an object from the swing class.
What would be my code in the actionPerformed button?
The next choice for example could be "swing"
When the user click on the OK button now I have to create an object from the swing class.
What would be my code in the actionPerformed button?
>>What would be my code in the actionPerformed button?
Object x = stringToClass.get((String) list.getSe lectedObje ct()).newI nstance();
Object x = stringToClass.get((String)
ASKER
CEHJ
there is something I don't understand because I have to include parameters in the creation of my object.
SwingTrader has to be replaced by the selection but I have to include destListModel and pointer parameters
SwingTrader st = new SwingTrader(destListModel, pointer);
there is something I don't understand because I have to include parameters in the creation of my object.
SwingTrader has to be replaced by the selection but I have to include destListModel and pointer parameters
SwingTrader st = new SwingTrader(destListModel,
ASKER
I don't want to have to write code like that :
if (system == "SwingTrader"){
SwingTrader st = new SwingTrader(destListModel, pointer);
} else{}
if (system == "Swing"){
Swing st = new Swing(destListModel,pointe r);
} else{}
etc ............
if (system == "SwingTrader"){
SwingTrader st = new SwingTrader(destListModel,
} else{}
if (system == "Swing"){
Swing st = new Swing(destListModel,pointe
} else{}
etc ............
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Make the SwingTrader a bean - give it an empty ctor and give it setListModel and setPointer methods
> Make the SwingTrader a bean - give it an empty ctor and give it setListModel and setPointer methods
unnecessary, they can be passed in the ctor as I've shown above
u could even store the Constructor in the list to save you some work.
You would use a custom list renderer to comtrol what is actually displayed in the list
unnecessary, they can be passed in the ctor as I've shown above
u could even store the Constructor in the list to save you some work.
You would use a custom list renderer to comtrol what is actually displayed in the list
No need to use reflection. Most Java classes should be designed as beans anyway as they will increase their compatibility with other APIs thereby
ASKER
objects
here is my modified code
private class OKListener implements ActionListener {
public void actionPerformed(ActionEven t e) {
Class clazz = (Class) system;
Constructor ctor = clazz.getDeclaredConstruct or(new Class[] { ListModel.class, Object.class});
Object o = ctor.newInstance(new Object[] {destListModel, pointer});
}
Now system is a String coming from a user selection.
I have the following error :
Cannot cast from String to Class
here is my modified code
private class OKListener implements ActionListener {
public void actionPerformed(ActionEven
Class clazz = (Class) system;
Constructor ctor = clazz.getDeclaredConstruct
Object o = ctor.newInstance(new Object[] {destListModel, pointer});
}
Now system is a String coming from a user selection.
I have the following error :
Cannot cast from String to Class
use the getSelectedValue() to get Object from list
eg.
Class clazz = (Class) list.getSelectedValue();
eg.
Class clazz = (Class) list.getSelectedValue();
>>Now system is a String coming from a user selection.
Object x = stringToClass.get(system). newInstanc e();
Object x = stringToClass.get(system).
If you don't want to store the Class objects in the list then use:
Class clazz = Class.forName(system);
Though more efficient to store Class (or Constructor's) in list
Class clazz = Class.forName(system);
Though more efficient to store Class (or Constructor's) in list
ASKER
I've got a number of unhandled exception
What would be my catch blocks
for example
catch(ClassNotFoundExcepti on ect){}
catch(NoSuchMethodExceptio n et){}
What would be my catch blocks
for example
catch(ClassNotFoundExcepti
catch(NoSuchMethodExceptio
> What would be my catch blocks
just:
catch (Exception ex) {
if you don't have specific actions to take for different exceptions
and if you do add exception blocks for the exceptions you want to handle seperately before it
just:
catch (Exception ex) {
if you don't have specific actions to take for different exceptions
and if you do add exception blocks for the exceptions you want to handle seperately before it
ASKER
Sorry
I am coming back to you because I had to work on somethig else
It still doesn't work. When I run the code nothing happens. When I call the constructor for class SwingTrader I should be executing a method.
Here is my code :
private class okbuttonListener implements ActionListener {
public void actionPerformed( ActionEvent e )
{
LinkedList list = new LinkedList();
for(int i = 0; i < destList.getModel().getSiz e(); i++) {
list.add(destList.getModel ().getElem entAt(i));
}
selectedsystem = txt.getText();
final LoadXMLConfiguration chemin;
chemin = new LoadXMLConfiguration("C:/X ML/chemin. xml",true) ;
String Lechemin = chemin.getDriver();
try{
Class clazz = Class.forName(selectedsyst em);
Constructor ctor = clazz.getDeclaredConstruct or(new Class[] { String.class, String.class, LinkedList.class, Object.class});
Object o = ctor.newInstance(new Object[] {Lechemin+"src/SteadFast/s ystems/Swi ngTrader.x ml","C:/XM L/chemin.x ml",list, pointer});
}
catch(Exception ex){}
dispose();
}
}
Do you see something from that code that is wrong ?
I am coming back to you because I had to work on somethig else
It still doesn't work. When I run the code nothing happens. When I call the constructor for class SwingTrader I should be executing a method.
Here is my code :
private class okbuttonListener implements ActionListener {
public void actionPerformed( ActionEvent e )
{
LinkedList list = new LinkedList();
for(int i = 0; i < destList.getModel().getSiz
list.add(destList.getModel
}
selectedsystem = txt.getText();
final LoadXMLConfiguration chemin;
chemin = new LoadXMLConfiguration("C:/X
String Lechemin = chemin.getDriver();
try{
Class clazz = Class.forName(selectedsyst
Constructor ctor = clazz.getDeclaredConstruct
Object o = ctor.newInstance(new Object[] {Lechemin+"src/SteadFast/s
}
catch(Exception ex){}
dispose();
}
}
Do you see something from that code that is wrong ?
> catch(Exception ex){}
change that so u know if something goes wrong
catch(Exception ex) { ex.printStackTrace(); }
Make sure selectedsystem is the fully qualified class name (ie. also contains package).
change that so u know if something goes wrong
catch(Exception ex) { ex.printStackTrace(); }
Make sure selectedsystem is the fully qualified class name (ie. also contains package).
ASKER
Here is the result when I include the package name :
java.lang.NoSuchMethodExce ption: systems.SwingTrader.<init> (java.lang .String, java.lang.String, java.util.LinkedList, java.lang.Object)
at java.lang.Class.getConstru ctor0(Unkn own Source)
at java.lang.Class.getDeclare dConstruct or(Unknown Source)
at GUI.SystemSelection$okbutt onListener .actionPer formed(Sys temSelecti on.java:37 2)
at javax.swing.AbstractButton .fireActio nPerformed (Unknown Source)
at javax.swing.AbstractButton $Handler.a ctionPerfo rmed(Unkno wn Source)
at javax.swing.DefaultButtonM odel.fireA ctionPerfo rmed(Unkno wn Source)
at javax.swing.DefaultButtonM odel.setPr essed(Unkn own Source)
at javax.swing.plaf.basic.Bas icButtonLi stener.mou seReleased (Unknown Source)
at java.awt.Component.process MouseEvent (Unknown Source)
at javax.swing.JComponent.pro cessMouseE vent(Unkno wn Source)
at java.awt.Component.process Event(Unkn own Source)
at java.awt.Container.process Event(Unkn own Source)
at java.awt.Component.dispatc hEventImpl (Unknown Source)
at java.awt.Container.dispatc hEventImpl (Unknown Source)
at java.awt.Component.dispatc hEvent(Unk nown Source)
at java.awt.LightweightDispat cher.retar getMouseEv ent(Unknow n Source)
at java.awt.LightweightDispat cher.proce ssMouseEve nt(Unknown Source)
at java.awt.LightweightDispat cher.dispa tchEvent(U nknown Source)
at java.awt.Container.dispatc hEventImpl (Unknown Source)
at java.awt.Window.dispatchEv entImpl(Un known Source)
at java.awt.Component.dispatc hEvent(Unk nown Source)
at java.awt.EventQueue.dispat chEvent(Un known Source)
at java.awt.EventDispatchThre ad.pumpOne EventForHi erarchy(Un known Source)
at java.awt.EventDispatchThre ad.pumpEve ntsForHier archy(Unkn own Source)
at java.awt.EventDispatchThre ad.pumpEve nts(Unknow n Source)
at java.awt.EventDispatchThre ad.pumpEve nts(Unknow n Source)
at java.awt.EventDispatchThre ad.run(Unk nown Source)
java.lang.NoSuchMethodExce
at java.lang.Class.getConstru
at java.lang.Class.getDeclare
at GUI.SystemSelection$okbutt
at javax.swing.AbstractButton
at javax.swing.AbstractButton
at javax.swing.DefaultButtonM
at javax.swing.DefaultButtonM
at javax.swing.plaf.basic.Bas
at java.awt.Component.process
at javax.swing.JComponent.pro
at java.awt.Component.process
at java.awt.Container.process
at java.awt.Component.dispatc
at java.awt.Container.dispatc
at java.awt.Component.dispatc
at java.awt.LightweightDispat
at java.awt.LightweightDispat
at java.awt.LightweightDispat
at java.awt.Container.dispatc
at java.awt.Window.dispatchEv
at java.awt.Component.dispatc
at java.awt.EventQueue.dispat
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
ASKER
I am increasing the points because I am stuck with this problem.
thats saying the class does not have a ctor that takes those args, what ctors does SwingTrader have?
ASKER
When I run this code :
Constructor con[]= Class.forName("systems.Swi ngTrader") .getDeclar edConstruc tor();
for (int x=0;x<con.length;x++)
System.out.println("Constr uctor "+x+" = "+con[x]);
}
catch(Exception ex){ ex.printStackTrace();}
I get :
Constructor 0 = public systems.SwingTrader()
Constructor 1 = public systems.SwingTrader(java.l ang.String ,java.lang .String)
Constructor 2 = public systems.SwingTrader(java.l ang.String ,java.lang .String,ja va.util.Li nkedList,q uotes.DBQu otes)
I have three constructors in my SwingTrader class :
Here they are :
/**
* Constructor 1
*/
public SwingTrader() {
this.systemName = "Break out of n days";
this.systemDescription = "Break Out system)";
this.systemAuthor = "Bernard Larouche";
this.showInfo();
}
/**
* Constructor 2
* @param configFile
*/
public SwingTrader(String configFile, String xmldirectory) {
super(configFile,xmldirect ory);
// TODO Auto-generated constructor stub
}
/**
* Constructor 3
* @param configFile
*/
public SwingTrader(String Config,String xmldir,LinkedList list, DBQuotes pointer) {
super(Config,xmldir,list,p ointer);
}
and Here my three constructors from the super class ASystem :
/**
* Constructor
*/
public ASystem(){};
/**
* Constructor
* @param symbol
*/
public ASystem(String configFile, String xmldirectory){
way = new LoadXMLConfiguration("C:/X ML/chemin. xml",true) ;
String theway = way.getDriver();
conf = new LoadXMLConfiguration(confi gFile,true );
this.configurationFile = configFile;
dir = new LoadXMLConfiguration(xmldi rectory,tr ue);
this.directory = xmldirectory;
// for each symbol in configuration file run this system
for (int i=0;i<conf.getSymbols().le ngth;i++){
this.symbol = conf.getSymbols()[i];
confcost = new LoadXMLConfiguration(thewa y+"jars/co nfig/commi ssions/"+s ymbol+"Spe c.xml",tru e);
this.symbolminmove = confcost.getMinMove();
executeSystem();
}
}
public ASystem(String configFile,String xmldirectory,LinkedList list, DBQuotes pointer){
this.LList = list;
this.pointer = pointer;
way = new LoadXMLConfiguration("C:/X ML/chemin. xml",true) ;
String theway = way.getDriver();
conf = new LoadXMLConfiguration(confi gFile,true );
this.configurationFile = configFile;
dir = new LoadXMLConfiguration(xmldi rectory,tr ue);
this.directory = xmldirectory;
// for each symbol in configuration file run this system
for (int i=0;i<LList.size();i++){
this.symbol = (String)LList.get(i);
confcost = new LoadXMLConfiguration(thewa y+"jars/co nfig/commi ssions/"+s ymbol+"Spe c.xml",tru e);
this.symbolminmove = confcost.getMinMove();
executeSystem();
}
}
Constructor con[]= Class.forName("systems.Swi
for (int x=0;x<con.length;x++)
System.out.println("Constr
}
catch(Exception ex){ ex.printStackTrace();}
I get :
Constructor 0 = public systems.SwingTrader()
Constructor 1 = public systems.SwingTrader(java.l
Constructor 2 = public systems.SwingTrader(java.l
I have three constructors in my SwingTrader class :
Here they are :
/**
* Constructor 1
*/
public SwingTrader() {
this.systemName = "Break out of n days";
this.systemDescription = "Break Out system)";
this.systemAuthor = "Bernard Larouche";
this.showInfo();
}
/**
* Constructor 2
* @param configFile
*/
public SwingTrader(String configFile, String xmldirectory) {
super(configFile,xmldirect
// TODO Auto-generated constructor stub
}
/**
* Constructor 3
* @param configFile
*/
public SwingTrader(String Config,String xmldir,LinkedList list, DBQuotes pointer) {
super(Config,xmldir,list,p
}
and Here my three constructors from the super class ASystem :
/**
* Constructor
*/
public ASystem(){};
/**
* Constructor
* @param symbol
*/
public ASystem(String configFile, String xmldirectory){
way = new LoadXMLConfiguration("C:/X
String theway = way.getDriver();
conf = new LoadXMLConfiguration(confi
this.configurationFile = configFile;
dir = new LoadXMLConfiguration(xmldi
this.directory = xmldirectory;
// for each symbol in configuration file run this system
for (int i=0;i<conf.getSymbols().le
this.symbol = conf.getSymbols()[i];
confcost = new LoadXMLConfiguration(thewa
this.symbolminmove = confcost.getMinMove();
executeSystem();
}
}
public ASystem(String configFile,String xmldirectory,LinkedList list, DBQuotes pointer){
this.LList = list;
this.pointer = pointer;
way = new LoadXMLConfiguration("C:/X
String theway = way.getDriver();
conf = new LoadXMLConfiguration(confi
this.configurationFile = configFile;
dir = new LoadXMLConfiguration(xmldi
this.directory = xmldirectory;
// for each symbol in configuration file run this system
for (int i=0;i<LList.size();i++){
this.symbol = (String)LList.get(i);
confcost = new LoadXMLConfiguration(thewa
this.symbolminmove = confcost.getMinMove();
executeSystem();
}
}
> Constructor ctor = clazz.getDeclaredConstruct or(new Class[] { String.class, String.class, LinkedList.class, Object.class});
should be
Constructor ctor = clazz.getDeclaredConstruct or(new Class[] { String.class, String.class, LinkedList.class, DBQuotes.class});
should be
Constructor ctor = clazz.getDeclaredConstruct
ASKER
objects
You are the best !!
Thank you
You are the best !!
Thank you
Map<String, Class> stringToClass = new HashMap<String, Class>();
stringToClass.put("StringT
Object x = stringToClass.get("StringT