org.j4me.ui.components
Class MenuOption

java.lang.Object
  extended by org.j4me.ui.components.Component
      extended by org.j4me.ui.components.MenuOption

public class MenuOption
extends Component

The Menu screen uses one of these MenuOption components for each menu choice.

The default implementation shows the choice number on the left, then the text for the choice, and an arrow on the right if it is a submenu. The text is clipped if it runs over a single line. This keeps all menu items the same height.

See Also:
Menu

Field Summary
 
Fields inherited from class org.j4me.ui.components.Component
container, HIGHLIGHTED_BORDER_WIDTH
 
Constructor Summary
MenuOption(DeviceScreen choice)
          Creates a new MenuOption component that encapsulates a DeviceScreen.
MenuOption(Menu choice, boolean submenu)
          Creates a new MenuOption component that encapsulates a Menu.
MenuOption(MenuItem choice)
          Creates a new MenuOption component that encapsulates a MenuItem.
MenuOption(java.lang.String text, DeviceScreen choice)
          Creates a new MenuOption component that encapsulates a DeviceScreen.
 
Method Summary
 boolean acceptsInput()
          Tells if this component accepts user input or not.
 java.lang.String getLabel()
           
protected  int[] getPreferredComponentSize(Theme theme, int viewportWidth, int viewportHeight)
          Returns the size of the menu choice.
protected  int[] getPreferredTextSize(Theme theme, int viewportWidth, int viewportHeight)
          Returns the size of the menu text.
protected  int[] getSubmenuIndicatorSize(Theme theme, int viewportWidth, int viewportHeight)
          Returns the size of the submenu indicator.
protected  void hideNotify()
          An event raised whenever the component is removed from the screen.
 boolean isSubmenu()
           
 void keyPressed(int key)
          Called when the user presses any key.
protected  void paintComponent(javax.microedition.lcdui.Graphics g, Theme theme, int width, int height, boolean selected)
          Paints a MenuOption.
protected  void paintText(javax.microedition.lcdui.Graphics g, Theme theme, int x, int y, int width, int height, boolean selected)
          Paints a the text within the menu option component.
 void pointerPressed(int x, int y)
          Called when the pointer is pressed.
 void select()
          Activates the command represented by this choice.
 void setLabel(java.lang.String label)
          Explicitly sets the text shown in this menu option.
protected  void showNotify()
          An event raised whenever the component is made visible on the screen.
 
Methods inherited from class org.j4me.ui.components.Component
getHeight, getHorizontalAlignment, getPreferredSize, getScreen, getWidth, getX, getY, invalidate, isShown, keyReleased, keyRepeated, paint, paintRect, pointerDragged, pointerReleased, repaint, setHorizontalAlignment, visible
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MenuOption

public MenuOption(MenuItem choice)
Creates a new MenuOption component that encapsulates a MenuItem.

Parameters:
choice - is the command that is represented by this component.

MenuOption

public MenuOption(DeviceScreen choice)
Creates a new MenuOption component that encapsulates a DeviceScreen.

Parameters:
choice - is the command that is represented by this component.

MenuOption

public MenuOption(java.lang.String text,
                  DeviceScreen choice)
Creates a new MenuOption component that encapsulates a DeviceScreen.

Parameters:
text - is string that appears in the menu option.
choice - is the command that is represented by this component.

MenuOption

public MenuOption(Menu choice,
                  boolean submenu)
Creates a new MenuOption component that encapsulates a Menu.

Parameters:
choice - is the command that is represented by this component.
submenu - when true indicates this is a submenu of the current menu.
Method Detail

isSubmenu

public boolean isSubmenu()
Returns:
true if this choice represents a submenu; false otherwise.

setLabel

public void setLabel(java.lang.String label)
Explicitly sets the text shown in this menu option. This overrides any value set by the MenuItem or the title of the DeviceScreen. To clear explicitly set text pass in null.

Parameters:
label - is the text that will appear for this menu item. If null the MenuItem.getText() or DeviceScreen.getTitle() will be used.

getLabel

public java.lang.String getLabel()
Returns:
The text displayed for this menu item. It is never null.

select

public void select()
Activates the command represented by this choice. The Menu class will call this method when the user selects this choice.


showNotify

protected void showNotify()
An event raised whenever the component is made visible on the screen. This is called before the paintComponent method.

Overrides:
showNotify in class Component

hideNotify

protected void hideNotify()
An event raised whenever the component is removed from the screen.

Overrides:
hideNotify in class Component

paintComponent

protected void paintComponent(javax.microedition.lcdui.Graphics g,
                              Theme theme,
                              int width,
                              int height,
                              boolean selected)
Paints a MenuOption. On the left is the choice number in a box. The middle has the text for the choice. If it is a submenu the right has an arrow.

Specified by:
paintComponent in class Component
Parameters:
g - is the Graphics object to paint with.
theme - is the application's theme used to paint the menu item.
width - is the width of the menu item area in pixels.
height - is the height of the menu item area in pixels.
selected - is true if this is the currently highlighted menu choice.
See Also:
Component.paintComponent(javax.microedition.lcdui.Graphics, org.j4me.ui.Theme, int, int, boolean)

paintText

protected void paintText(javax.microedition.lcdui.Graphics g,
                         Theme theme,
                         int x,
                         int y,
                         int width,
                         int height,
                         boolean selected)
Paints a the text within the menu option component.

Parameters:
g - is the Graphics object to paint with.
theme - is the application's theme used to paint the menu item.
x - is the left of the text area.
y - is the top of the text area.
width - is the width of the menu item area in pixels.
height - is the height of the menu item area in pixels.
selected - is true if this is the currently highlighted menu choice.
See Also:
paintComponent(Graphics, Theme, int, int, boolean)

getPreferredComponentSize

protected int[] getPreferredComponentSize(Theme theme,
                                          int viewportWidth,
                                          int viewportHeight)
Returns the size of the menu choice. It will be one line of text high and as wide as the screen.

Specified by:
getPreferredComponentSize in class Component
Parameters:
theme - is the application's Theme.
viewportWidth - is the width of the viewable area, in pixels, the component can use.
viewportHeight - is the height of the viewable area, in pixels, the component can use.
Returns:
A array with two elements where the first is the width of the component in pixels and the second is the height.
See Also:
Component.getPreferredComponentSize(org.j4me.ui.Theme, int, int)

getPreferredTextSize

protected int[] getPreferredTextSize(Theme theme,
                                     int viewportWidth,
                                     int viewportHeight)
Returns the size of the menu text.

Parameters:
theme - is the application's Theme.
viewportWidth - is the width of the screen in pixels.
viewportHeight - is the height of the screen in pixels.
Returns:
An array with two elements where the first is the width in pixels and the second is the height.

getSubmenuIndicatorSize

protected int[] getSubmenuIndicatorSize(Theme theme,
                                        int viewportWidth,
                                        int viewportHeight)
Returns the size of the submenu indicator. It appears at the right of this component.

Parameters:
theme - is the application's Theme.
viewportWidth - is the width of the screen in pixels.
viewportHeight - is the height of the screen in pixels.
Returns:
An array with two elements where the first is the width in pixels and the second is the height.

acceptsInput

public boolean acceptsInput()
Description copied from class: Component
Tells if this component accepts user input or not. If it does then it can be scrolled to by the user. If it does not, it will be displayed, but can be skipped over by scrolling.

The default implementation returns false. Override this method to return true if the component accepts input.

Overrides:
acceptsInput in class Component
Returns:
true because this component accepts user input.

keyPressed

public void keyPressed(int key)
Called when the user presses any key.

Overrides:
keyPressed in class Component
Parameters:
key - is code of the key that was pressed.
See Also:
Component.keyPressed(int)

pointerPressed

public void pointerPressed(int x,
                           int y)
Called when the pointer is pressed.

Overrides:
pointerPressed in class Component
Parameters:
x - is the horizontal location where the pointer was pressed relative to the top-left corner of the component.
y - is the vertical location where the pointer was pressed relative to the top-left corner of the component.