Solved

Any idea how to do this?

Posted on 2001-07-29
2
152 Views
Last Modified: 2010-03-31
hi there,
I'm new in Java programming, so i don't know either i can do this in Java programming. What i'm trying to do is...

I want create a class that will dynamically create Menu based on the information store in a database. This mean every time i call the function in the class, the function will retrieve data from database such as Menu Description and create the menu accordingly. What i have in my idea is i will have 3 class. The First is used to create the menu, the second one is a main class file which will call the createMenu method in the first class file and the third act as Menu Listener.
Actually i had successfully finish the first and the second part but my problem here is how can i create another class file that act as the listner for all the menu item created?

i would be very grateful if somebody out there can help..
thanks a lot..

0
Comment
Question by:pleasure
[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
2 Comments
 
LVL 7

Accepted Solution

by:
tomboshell earned 100 total points
ID: 6333322
The tricky part is recognizing the text stored in the menu items when using the Listener.  I usually extend the ActionListener (new class) and implement the public void actionPerformed(ActionEvent e) method.  In there a comparisson is made with the event triggered (e) and the stored string array containing the menu items.  Somewhere you must know what the string values to be expected are.  You could implements a string comparrison for all possible values, when one hits then goto a switch statement for executing the appropriate method.  Then declare an instance of the action handler class in your menu creation class and add the action listener with handler to each item.  ie (item1.addActionListener(myHandler);)  
could be something like this....
public class FileEvents implements ActionListener {
     String[] fileNames;
     Tester t;
     public FileEvents(String[] names, Tester test){
          fileNames=names;
          t=test;
     }
     public void actionPerformed(ActionEvent e){
         
          String name=e.getActionCommand();
          System.out.println("the value is =" + name);
          int i=0;
          while (name.compareToIgnoreCase(fileNames[i]) != 0){
               i++;
          }
          switch(i){
               case 0: t.newAction();
                         break;
               case 1: t.openEditAction();
                         break;
               case 2: t.openViewAction();
                         break;
               case 3: t.saveAction();
                         break;
               case 4: t.saveAsAction();
                         break;
               case 5: t.closeAction();
                         break;
               case 6: int confirm=JOptionPane.showConfirmDialog(null,"Are you sure you want to Exit the program?",
               "Exit Confirmation", JOptionPane.YES_NO_CANCEL_OPTION);
                         if (confirm==JOptionPane.YES_OPTION) System.exit(0);
                         else break;
               default: break;

          }
     }
}

Tom
0
 

Author Comment

by:pleasure
ID: 6698138
since there is no response from other experts, i decided to close the question.

Tombo:
since you are the only one reply to me... so i award u the point for your sincerity..thanks..:)
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn different types of Android Layout and some basics of an Android App.
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

751 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