I know this has been questioned before on stackoverflow, but I could not find a solution for my problem. I want to separate some Code from ButtonClick event, where I dynamically create Nodes on ButtonClick and add them to a parent AnchorPane. On my Nodes are Buttons, these Events on my Button are handled with a CommandPattern. I create different ButtonEvents, depending on the Node I created.
The Code which works fine so far is:
@FXML
void addErosionNode(ActionEvent event){
DragNode nde = new DragNode();
/*
id = nde.getId();
name = new String("Erosion");
Erosion cmd;
cmd = new Erosion();
nodeList.add(new NodeList<String, String, Command>(name, id, cmd));
*/
setupNode(nde);
nde.setNodeWithTwoCircles();
}
But I want to put the Code between /**/ inside another Method, so I can replace the Code with setupNode(nde, name);
And try to use this method:
public void setupNode(DragNode nde, String name){
id = nde.getId();
Class clazz;
className = new String ("application.bvfunc." + type);
//This will be e.g. application.bvfunc.Erosion which is the class I want to use
try {
clazz = Class.forName(className);
} catch (ClassNotFoundException e) {
System.out.println("fail");
}
clazz cmd;
cmd = new clazz();
nodeList.add(new NodeList<String, String, Command>(name, id, cmd));
nde.nodeLayout();
rightAnchor.getChildren().add(nde);
buildDragHandlers();
}
But clazz
cannot be resolved to a type.
How can I replace
Erosion cmd;
cmd = new Erosion();
with clazz
I create with the name of my Node?
Like this:
clazz cmd;
cmd = new clazz();