Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 158
  • Last Modified:

Any idea how to do this?

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
pleasure
Asked:
pleasure
1 Solution
 
tomboshellCommented:
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
 
pleasureAuthor Commented:
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now