aspose file tools*
The moose likes Beginning Java and the fly likes Execute the contents of a String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Execute the contents of a String" Watch "Execute the contents of a String" New topic
Author

Execute the contents of a String

Adelaide Clark
Greenhorn

Joined: Mar 25, 2001
Posts: 2
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

Joined: Jan 07, 1999
Posts: 6920
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:


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Adelaide Clark
Greenhorn

Joined: Mar 25, 2001
Posts: 2
Thank you. I appreciate your reply. I will try it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Execute the contents of a String
 
Similar Threads
how to detect that browser is going to issue a write command
Problem in adding node to JTree
Design question.
Executing a batch of SQL commands
can i have tomcat 4 and tomcat 5 together ?