Solved

Loading method names into a combo box

Posted on 2004-04-12
8
311 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 30

Expert Comment

by:Mayank S
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:Mayank S
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

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


Regards,
Muruga
0
 
LVL 30

Expert Comment

by:Mayank S
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:Mayank S
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
iterator/ListIterator approach 17 54
login form jsp example 2 53
add projects t working set in maven 2 44
Java syntax, or is it Selenium 6 42
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

726 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