Go Back   Java Programming Forums > Java Standard Edition Programming Help > Collections and Generics


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 21-09-2009, 07:55 AM
Junior Member
 

Join Date: Sep 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Swiftslide is on a distinguished road
Default Any way to map method calls?

I was wondering whether it was possible to have method calls as the value in a HashMap (or LinkedHashMap). For example, I'm designing a virtual PDA program for a uni assignment. One of my classes is a 'Menu' class which acts as sort of a template for the PDA class's menus. What I'm aiming for is a HashMap in the Menu class, where the key is the menu option (type String) and the value is the method called by selecting that option. The PDA class will then create a bunch of Menus, and fill each Menu's map with the Menu options and the methods called by those options.

Basically I think I want a field like this in the Menu class:

private HashMap<String, [Method Call]> menuOptions


And then have a method in the PDA class that does something like this:

menuOptions.put("Option A", doSomething());

where 'Option A' is the name of the option, and 'doSomething()' is a method signature.


Thanks in advance to anyone who can help me.



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 21-09-2009, 09:37 AM
Json's Avatar
Super Moderator
 

Join Date: Jul 2009
Location: Manchester, United Kingdom
Posts: 1,157
Thanks: 54
Thanked 136 Times in 132 Posts
Json will become famous soon enoughJson will become famous soon enoughJson will become famous soon enough

I'm feeling Happy
Default Re: Any way to map method calls?

Well you could rather just have an interface like for instance called MenuOption with a method on it.

Java Code
public interface MenuOption {
    public void execute();
}
Then you would create your menuoptions to implement this interface and then store the reference to that object in the map.

Java Code
public class MyMenuOption implements MenuOption {

    public void execute() {
        System.out.println('Execute in MyMenuOption was invoked');
    }
}
Java Code
    Map<String, MenuOption> menuOptions = new HashMap<String, MenuOption>();

    MyMenuOption myMenuOption = new MyMenuOption();

    menuOptions.put("Option A", myMenuOption);
And then in the menu whenever someone clicks a menu item.

Java Code
    MenuOption clickedMenuOption = menuOptions.get(keyOfMenuOption);
    clickedMenuOption.execute();
Something like that might do.

// Json
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Declare a method mohsendeveloper Object Oriented Programming 4 11-06-2009 08:18 AM
Display the content method? rocafella5007 What's Wrong With My Code? 1 30-04-2009 04:46 PM
problem with nextLine() method... mia_tech File I/O & Other I/O Streams 1 10-03-2009 08:14 AM
matches() method is not available in Jbuilder 4 IDE. sah Java SE APIs 2 04-03-2009 10:32 AM
matches() method is not available in Jbuilder4 sah Java IDEs 1 30-12-2008 11:30 AM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming practice problems java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

All times are GMT. The time now is 02:03 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.