• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Execute the contents of a String

 
Adelaide Clark
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Frank Carver
Sheriff
Posts: 6920
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Adelaide Clark
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I appreciate your reply. I will try it.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic