0

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();
Flippy
  • 202
  • 3
  • 12

2 Answers2

0

Replace

clazz cmd;
cmd = new clazz();

with

Command cmd = (Command) clazz.newInstance();
Dmitry T.
  • 673
  • 7
  • 7
  • I want to use it independent from Erosion, Dilatation etc. Dilatation is another class which contains a CommandPattern, so Erosion/Dilatation should be used from the String. When I use `Command` i get "cannot convert from Object to Command" – Flippy Dec 08 '16 at 10:16
  • @Dimitry T. I still get `Unhandled exception type IllegalAccessException` – Flippy Dec 08 '16 at 10:21
  • If you get an exception, the clazz remains uninitialized, then there is no much sense to continue (unless you have some alternative for a case the class is not found). So I would propose to remove this try ... catch ... at all, just call clazz = ... in the main setupNode code block. – Dmitry T. Dec 08 '16 at 10:29
  • When I remove it I get `Unhandled exception type ClassNotFoundException` at `clazz = Class.forName(className);` and `Unhandled exception type IllegalAccessException` at `cmd = (Command) clazz.newInstance();` – Flippy Dec 08 '16 at 10:32
  • Check whether the class name is correct and if the class can be found. Actually this is out of scope of your original question. – Dmitry T. Dec 08 '16 at 10:35
  • It says `clazz = Class.forName(className);` class not found, but the name of the class is right, it is in the folder application.bvfunc .. but using try/catch gives out my `System.out.prinln("success");` – Flippy Dec 08 '16 at 11:02
  • There is no such code in the snippet above... If you put the println after the try... catch it does not mean the class is found, it just means that you successfully ignored the problem. – Dmitry T. Dec 08 '16 at 13:14
0

You can, instead of using reflection, create a map that maps the names of their commands to the constructors:

private static final Map<String, Supplier<Command>> commandMap = new HashMap<>();

static {
    commandMap.put("Erosion", Erosion::new);
    commandMap.put("Dilation", Dilation::new);
}

Then in your other code:

public void setupNode(DragNode nde, String name){
    id = nde.getId();
    Command cmd = commandMap.get(name).get(); // <-- Calls the constructor             
    nodeList.add(new NodeList<String, String, Command>(name, id, cmd));

    nde.nodeLayout();
    rightAnchor.getChildren().add(nde);
    buildDragHandlers();
}
Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93