How can I execute the contents of a String? I have an unusual JTree in that a node is actually a java command. When a node is selected, I want to execute the command that is the name of the node. I know that I could keep an array of all the commands and obtain the count of how many next siblings there are and, thereby, know which node is selected and use it in a bunch of if statements -- or do a comparison until I find a match, also, using if/case statements. While this is possible, it is cumbersome. Isn't there some way I can just make the contents an executable statement?
While this is possible using the Java Reflection API, it's not easy, and usually not a good idea. The usual way to do this is to actually store an object instead of a string with each of your nodes. This object can be anything as long as they all implement the same interface, for example: