[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Loading method names into a combo box

Posted on 2004-04-12
8
Medium Priority
?
317 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: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 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month20 days, 10 hours left to enroll

868 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