Solved

Loading method names into a combo box

Posted on 2004-04-12
8
308 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:copyPasteGhost
  • 4
  • 2
  • 2
8 Comments
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10810560
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
 
LVL 9

Accepted Solution

by:
mmuruganandam earned 250 total points
ID: 10810607
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
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10810633
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
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10810647
Let me be careful hereafter.  Thanx for alerting me Mayank


Regards,
Muruga
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 30

Expert Comment

by:mayankeagle
ID: 10810652
:-)
0
 
LVL 13

Author Comment

by:copyPasteGhost
ID: 10813244
>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
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10813635
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
 
LVL 13

Author Comment

by:copyPasteGhost
ID: 10814944
ok thanks for the tip
Ghost
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now