Loading method names into a combo box

hello all,

I had this idea that I need some help with. I want to make a class to be able to test methods.
There is a textfield, a combo box, a textarea and 2 buttons.(reload and run)

in the textfield you will enter the name of the class. once you have entered a class name you can click on the reload button and this button will search that class and load all the public methods that it finds into the combo box. you can then enter any parameters that you need to enter in the textarea and the press the run button which will run the method with the specificed parameters..

the part I am having problems with is this part as you can imagine...

>> once you have entered a class name you can click on the reload button and this button will
>> search that class and load all the public methods that it finds into the combo box

if anyone can help me with this that would be excellent!
thanks,
Ghost
LVL 13
copyPasteGhostAsked:
Who is Participating?
 
mmuruganandamConnect With a Mentor Commented:
import java.lang.reflect.Method;

public class MethodNameFinder
{
      public MethodNameFinder()
      {
      }

      public String[] findMethods(String className) throws ClassNotFoundException
      {
            Class claz = Class.forName(className);
            Method[] methods = claz.getDeclaredMethods();

            if (methods != null)
            {
                  String[] methodNames = new String[methods.length];

                  for (int i = 0; i < methods.length; i++)
                  {
                        methodNames[i] = methods[i].getName();
                  }

                  return methodNames;
            }

            return null;
      }

      public static void main(String[] args) throws ClassNotFoundException
      {
            MethodNameFinder finder = new MethodNameFinder();
            String names[] = finder.findMethods("java.util.Vector");

            if (names != null)
            {
                  for (int i = 0; i < names.length; i++)
                  {
                        System.out.println(names[i]);
                  }
            }
      }
}


Hope this helps!


Regards,
Muruga
0
 
Mayank SAssociate Director - Product EngineeringCommented:
You need to use reflection for doing that. Add an action-listener to the button in which, you should read the class-name from the text-field, get a list of the members and display it in the combo-box.

Have a look at the reflection examples here:

http://www.javaalmanac.com/egs/java.lang.reflect/pkg.html?l=find
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Muruga, again the code.............. ;-) don't give full code just like that if somebody has not even asked for it. It also violates the member-agreement and the policies regarding home-work. As for this question, it might be homework, so you can't really say if we should be giving full-code like that or not. The code that you've posted is almost the same which you will find on one of the examples on the link that I posted.

rgds,
Mayank.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
mmuruganandamCommented:
Let me be careful hereafter.  Thanx for alerting me Mayank


Regards,
Muruga
0
 
Mayank SAssociate Director - Product EngineeringCommented:
:-)
0
 
copyPasteGhostAuthor Commented:
>mayankeagle
Don't worry it's not homework. I'm doing testing on a program for my company and I thought that this would make my job much easier. I understand your concern though.

>Muruga
ok I tried your code, thanks for the code by the way...code is always easier to read then pointless text that never gets to the point.
and it's perfect to say the least..

maybe you can help me with some other thing I wanted to do.. I think it's along the same lines... :c)

I want to write a program that will search a floder on my computer and find all the .mp3 files.. (I want to make a list of all my files, since I have several thousand I think writing something to do that can help.
What I want is this program to start searching at a particular folder(that we will pass eg. c:/myMusic/rock) in which case all the .mp3 files (including those in subfloders) will be found and a report generated..

the report can just be the file name for now .... later on I will explore how to read the id3 tag at the beginning of the mp3 to make a better report but for now all I need is the file name

I hope that's clear.
Please let me know if you don't understand,
cheers
Ghost
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Hi Ghost,

Well, glad to know that its not homework.

>> code is always easier to read then pointless text that never gets to the point

That is what is there at the link which I posted as well - its one of the largest Java-examples sites.

Generally, when you get a solution to the question you asked, its not good practice at EE to stretch the question beyond its initial scope. Your initial question dealt with classes and their methods, and their loading into a combo-box. Searching files in a folder is a totally different topic. I suggest that you should close this question (accept an answer or split points, if comments from both the experts were relevant), and ask the file-search thing in a new question.

For a start, I will give you a slight idea. Make a java.io.File object over the directory-name.

Documentation on: http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html

Examples on:

http://www.javaalmanac.com/egs/java.io/GetFiles.html 

http://www.javaalmanac.com/egs/java.io/TraverseTree.html?l=rel (also have a look at the "Related Examples").

Get a list of all files in the directory and check if the names end with ".mp3". If it does, then add it to a list or something.
0
 
copyPasteGhostAuthor Commented:
ok thanks for the tip
Ghost
0
All Courses

From novice to tech pro — start learning today.