Solved

Any idea how to do this?

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

743 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

13 Experts available now in Live!

Get 1:1 Help Now