| 
 | MIDP3.0 | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjavax.microedition.lcdui.Menu
public class Menu
 A visual container for Commands and other Menus.
 The Menu class is intended to be used in association with
 exact placement of commands to group
 together commands that do not fit on the given Display
 or should be grouped together for usability reasons.
 
 A Menu object is always created empty, and later populated with
 commands using either the append() or insert() methods.
 A given Command may only be added once to a given Menu, but
 can be added to more than one Menu.
Versions of these methods are also provided for adding sub-menus to a menu, thereby enabling the creation of a menu tree. A menu may have multiple menus added to it as sub-menus, but a menu may be added to no more than one parent menu.
 It is up to the implementation how to handle manipulation (addition/removal) of menu content
 when a menu is visible. The application may use the isVisible() method to
 find out if the Menu is presently visible on any Display.
 
 The platform may limit the depth of sub-menus on a Display. The present depth of a menu, may be
 retrieved by the getMenuDepth() method. The device-wide maximum menu depth is returned by the
 getMaxMenuDepth() method; the implementation MUST support a menu depth of up
 to five(5) menus, but MAY support a higher menu depth.
 
 Menus are added to a Displayable with the
 Displayable.setMenu method. The same
 Menu can be added to multiple Displayables at the same time. However,
 the platform MUST ensure that only the menu belonging to the Displayable currently
 in focus is shown. This means that the same menu button may appear on multiple
 Displays, but the menu itself can only be shown on one Display at a time.
 A Menu may generally be placed on the same exact placements as Commands.
 However, setMenu will throw
 java.lang.IllegalArgumentException when the attempted placement
 is not allowed. The reason for failure can be that the particular placement
 does not support menus. Doing additional setMenu to a
 Displayable with different placement will move the Menu to the new placement.
 
 Note that a there is a difference between Displayable.setMenu(Menu) and
 Menu.append(Menu); A single Menu object may be added to multiple Displayables,
 but only be shown on one at the same time.
 The same Menu object may only be appended to one menu at any time.
 
As an example of using the Menu class, consider the following example. The developer wants to set up a menu tree consisting of a top menu with two levels of sub menus.
 Menu m, sm1, sm2;
 Command cmd1, cmd2, cmd3;
 Displayable d;
 m = new Menu("Menu", "This is the top menu", menupict.gif);
 sm1 = new Menu("sub-menu 1", "This is the middle sub-menu", menupict.gif);
 sm2 = new Menu("sub-menu 2", "This is the deepest sub-menu", menupict.gif);
 cmd1 = new Command("Command 1", "Command 1", Command.ITEM, 1);
 cmd2 = new Command("Command", "Second", Command.ITEM, 1);
 cmd3 = new Command("Third", "Third", Command.ITEM, 1);
 // Creating the menu structure
 m.append(cmd1);
 m.append(sm1);
 sm1.append(cmd2);
 sm1.append(sm2);
 sm2.append(cmd3);
 // Add the Menu to the Displayable on placement 101 (lower left corner).
 d.setMenu(m, 101);
 
 The full structure of the menu above is shown in the following picture:
  
  
 
 A menu is initially not visible, but is brought into view only as it is
 selected by the user, typically by pressing a associated soft key.
 The implementation must provide the ability to navigate a menu, to
 select a command in a menu, and to allow the user to
 leave the menu without selecting anything (this is required for all
 displays that support menus). When a Commmand is selected,
 it is passed to the CommandListener of the Displayable
 of the Display where the Menu is presently visible.
 It is up to the implementation to associate the correct
 Displayable with a particular Menu when it is
 to be used.When a command is selected, or the users navigates out of the
 menu, the menu should become invisible as soon as possible.
 
A Menu MUST NOT be visible on more than one Display at a time. If a Menu is visible on a Display that loses focus, then the Menu should be made invisible as soon as possible, and if the that Display should regain focus, the Menu should again start out as invisible. There MUST NOT be any interaction capability with a Menu that remains visible after its Display has lost focus.
The application may add an Image to the Menu to be shown together with the labels. The device shows the image on a best effort basis, depending on what can be done on the particular display. For example, a Menu with an image is added to two different displays, one with high resolution and one with low, the implementation may decide either to show the image only on the high resolution display or ignore the image completely. Implementations may truncate or scale the icon image if it is larger than the size supported by device.
The Image may be mutable or immutable. If the Image is mutable,
 then a snapshot of its contents is taken before the Menu()
 constructor or setImage() method returns.
 The snapshot is used whenever the contents of the Menu are to be displayed.
 Even if the application subsequently draws into the Image,
 the snapshot is not modified until the next call to setImage().
 If the Menu is visible on the display then
 the display SHOULD be updated with the new snapshot as soon as it is
 feasible for the implementation to do so. 
 Applications can query the implementation's Menu icon size by calling
 Display.getBestImageWidth(int) and Display.getBestImageHeight(int) methods
 using the Display.MENU image type. The style and appearance of
 menus are platform-dependent. If the image is changed after the Menu is
 first displayed, then it is up to
 the implementation to act on the change as soon as possible.
 
| Constructor Summary | |
|---|---|
| Menu(java.lang.String shortLabel,
     java.lang.String longLabel,
     Image image)Creates a new Menuobject with the given labels and image. | |
| Method Summary | |
|---|---|
|  int | append(Command cmd)Appends a Command to the Menu. | 
|  int | append(Menu menu)Appends a sub-menu to this Menu. | 
|  Command | getCommand(int index)Gets the command at index. | 
|  Font | getFont()Gets the application's preferred font for rendering the labels for this Menu. | 
|  Image | getImage()Gets the Image for this Menu. | 
|  java.lang.String | getLabel()Gets the label of the Menu. | 
|  java.lang.String | getLongLabel()Gets the long label of the Menu. | 
| static int | getMaxMenuDepth()Returns the device-wide maximum menu depth. | 
|  Menu | getMenu(int index)Gets the sub-menu at index. | 
|  int | getMenuDepth()Returns this menu's depth within its menu tree. | 
|  void | insert(int index,
       Command cmd)Inserts a Commandinto theMenujust prior to the
 element specified. | 
|  void | insert(int index,
       Menu menu)Inserts a Menuinto theMenujust prior to the
 element specified. | 
|  boolean | isCommand(int index)Checks if the element at the indicated index is a Command. | 
|  boolean | isEnabled()Gets a boolean value indicating whether the Menu is enabled or disabled. | 
|  boolean | isVisible()Gets a boolean value indicating whether the Menu is presently visible on any Display. | 
|  void | onParentEnabled(boolean enabled)Informs the menu of a change in its parent's enabledvalue. | 
|  void | remove(Command cmd)Removes a command from the menu. | 
|  void | remove(Menu menu)Removes a sub-menu from the menu. | 
|  void | setEnabled(boolean isEnabled)Sets the Menu into enabled or disabled mode. | 
|  void | setFont(Font font)Sets the application's preferred font for rendering the labels for this Menu. | 
|  void | setImage(Image image)Sets the Image for this Menu. | 
|  void | setLabel(java.lang.String shortLabel)Sets the short label of the Menu. | 
|  void | setLongLabel(java.lang.String longLabel)Sets the long label of the Menu. | 
|  int | size()Gets the number of Commands and sub-Menus in the Menu. | 
| Methods inherited from class java.lang.Object | 
|---|
| equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait | 
| Constructor Detail | 
|---|
public Menu(java.lang.String shortLabel,
            java.lang.String longLabel,
            Image image)
Menu object with the given labels and image.
 
 The short label is required and must not be null. The
 long label is optional and may be null if the Menu is
 to have no long label. The labels are used like the labels on commands,
 they may also be used by the implementation as descriptive headers on the menu when it
 is being shown.
 
shortLabel - the Menu's short labellongLabel - the Menu's long label, or null if noneimage - the Menu's image, or null if none
java.lang.NullPointerException - if shortLabel is null| Method Detail | 
|---|
public void remove(Command cmd)
java.lang.IllegalArgumentException - if cmd is not in menupublic void remove(Menu menu)
menu - the Menu to remove
java.lang.IllegalArgumentException - if menu is not in menupublic int getMenuDepth()
Displayable
 and another menu, its depth is calculated based on the menu to which it is attached.
 For instance, a menu A is a sub-menu of menu B and is also attached to a Displayable.
 If menu B is of depth 3, then menu A will return a depth of 4.
public static int getMaxMenuDepth()
public int append(Command cmd)
cmd - the command to be added
java.lang.NullPointerException - if cmd is null
java.lang.IllegalArgumentException - if the cmd is already added to this menu.public int append(Menu menu)
menu - the menu to be added
java.lang.NullPointerException - if menu is null
java.lang.IllegalArgumentException - if the menu already has a parent menu or
         if it is the top menu in this menu's tree.
java.lang.IllegalStateException - if the resulting menu depth exceeds the maximum
         depth supported by the implementation.  The menu depth includes all of
         the menus above this menu, the menu being inserted, and all of its sub-menus.public boolean isEnabled()
public boolean isVisible()
Display.
 Note that this method does NOT indicate which Displayable that is currently shown on the Display in focus. Being visible means that the Menu or one of its sub-menus is being browsed.
public void setEnabled(boolean isEnabled)
isEnabled - true if enabled, false if disabledpublic Command getCommand(int index)
index parameter must be within the range
 [0..size()-1], inclusive.
java.lang.IndexOutOfBoundsException - if index is less than zero or
          greater than or equal to size().
java.lang.IllegalArgumentException - if element at index is not a commandpublic Menu getMenu(int index)
index parameter must be
 within the range [0..size()-1], inclusive.
java.lang.IndexOutOfBoundsException - if index is less than zero or
          greater than or equal to size().
java.lang.IllegalArgumentException - if element at index is not a menupublic Font getFont()
Menu. The value returned is the font that had
 been set by the application, even if that value had been disregarded by
 the implementation. If no font had been set by the application, or if the
 application explicitly set the font to null, the value is
 the default font chosen by the implementation.
public void setFont(Font font)
Menu. Note that the set font is for the Menu object itself,
 and not for the Commands and Menus it may contain. An menu's font is a
 hint, and the implementation may disregard the application's preferred
 font. Note that this method does not influence the fonts used by the
 contents (commands and sub-menus) of the menu, and these must be set
 individually using the respective setFont(Font) methods.
 
 The font parameter must be a valid Font
 object or null. If the font parameter is
 null, the implementation must use its default font to
 render the element.
 
font - the preferred font to use to render the Menu's labels
public void insert(int index,
                   Command cmd)
Command into the Menu just prior to the
 element specified. The size of the Menu grows by one.
 The index parameter must be within the range
 [0..size()], inclusive.  If the value of
 index equals size(), the new element is
 inserted immediately after the last element; in this case,
 the effect is identical to calling append().
index - the index of the element where insertion is to occur, starting from zerocmd - the command to be inserted
java.lang.IndexOutOfBoundsException - if index is less than zero or
             greater than size().
java.lang.NullPointerException - if cmd is null
java.lang.IllegalArgumentException - if the cmd is already added to this menu.
public void insert(int index,
                   Menu menu)
Menu into the Menu just prior to the
 element specified. The size of the Menu grows by one.
 The sub-menu can only be in one menu at a time.
 The application is responsible for removing the sub-menu from a
 previous menu, if any, before it can be inserted into a new menu.
 The index parameter must be within the range
 [0..size()], inclusive.  If the value of
 index equals size(), the new element is
 inserted immediately after the last element; in this case,
 the effect is identical to calling append().
index - the index of the element where insertion is to occur, starting from zeromenu - the menu to be inserted
java.lang.IndexOutOfBoundsException - if index is less than zero or
         greater than size().
java.lang.NullPointerException - if menu is null
java.lang.IllegalArgumentException - if the menu already has a parent menu or
         if it is the top menu in this menu's tree.
java.lang.IllegalStateException - if the resulting menu depth exceeds the maximum
         depth supported by the implementation.  The menu depth includes all of
         the menus above this menu, the menu being inserted, and all of its sub-menus.public boolean isCommand(int index)
Command.
 The index parameter must be within the range
 [0..size()-1], inclusive. The index of the last element is
 size()-1
Command, or false if it is a Menu
java.lang.IndexOutOfBoundsException - if index is less than zero or
             greater than or equal to size().public Image getImage()
null if there is
         no imagepublic void setImage(Image image)
image - the Image for this menu or
      null if the menu is not to have an Imagepublic java.lang.String getLabel()
public java.lang.String getLongLabel()
null if there is no long labelpublic void setLongLabel(java.lang.String longLabel)
longLabel - the long label, or null if there is no long labelpublic void setLabel(java.lang.String shortLabel)
null.
shortLabel - the short label
java.lang.NullPointerException - if label is nullpublic int size()
Menupublic void onParentEnabled(boolean enabled)
enabled
 value. The application must itself decide how the Menu should handle
 this information. The default is to do nothing.
| 
 | MIDP3.0 | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||