Solved

Any idea how to do this?

Posted on 2001-07-29
2
147 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 83
mockito example issue 8 64
diffSum example 4 27
egit plugin on eclipse 8 41
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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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 …

863 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

22 Experts available now in Live!

Get 1:1 Help Now