Package totalcross.ui

Class MenuBar


  • public class MenuBar
    extends Window
    Constructs a Menu with the given items. A menu can be opened by the user in a couple of ways:
    • By clicking in the Menu button
    • By clicking in the title of a Window
    The Menu supports disabled and checked items. Here is an example of how to build the menu:
     MenuItem miBeamEvent,miNewEvent,miDeleteEvent;
     MenuItem col0[] =
     {
       new MenuItem("Record"),                               // caption for the MenuBar
       miNewEvent = new MenuItem("NewEvent",false),          // checked item, starting unchecked
       miDeleteEvent = new MenuItem("Delete Event...",true), // checked item, starting checked
       new MenuItem("Attach Note"),
       new MenuItem("Delete Note..."),
       new MenuItem("Purge..."),
       beamEvent = new MenuItem("Beam Event"),
     };
     MenuItem col1[] =
     {
       new MenuItem("Edit"),           // caption for the MenuBar
       new MenuItem("Undo"),
       new MenuItem("Cut"),
       new MenuItem("Copy"),
       new MenuItem("Paste"),
       new MenuItem("Select All"),
       new MenuItem(),                 // a separator
       new MenuItem("Keyboard"),
       new MenuItem("Graffiti Help"),
     };
     MenuItem col2[] =
     {
       new MenuItem("Options"),        // caption for the MenuBar
       new MenuItem("Font..."),
       new MenuItem("Preferences..."),
       new MenuItem("Display Options..."),
       new MenuItem("Phone Lookup"),
       new MenuItem("About Date Book"),
     };
     setMenuBar(new MenuBar(new MenuItem[][]{col0,col1,col2}));
     beamEvent.isEnabled = false;
     ...
     // at later time, disable the "new event" and enable the "delete event"
     miNewEvent.isChecked = true;
     miDeleteEvent.isChecked = false;
     
    The menu can be closed by a click on a valid item or clicking outside of its bounds. A PRESSED event will be thrown when the menu is closed and a menu item was selected. To discover which item was selected, see method getSelectedIndex, which returns -1 if none, or the matrix index otherwise.

    Note that the separator dotted line doesn't generate events and can't be selected.

    To convert the old menu form (using a string matrix) into the new form using a MenuItem matrix, you can use this idea:
    Old format:

     // the string arrays menuArq, menuUtil and menuSobre are initialized somewhere else
     setMenuBar(mbar = new MenuBar(new String[][]{menuArq,menuUtil,menuSobre}));
     mbar.setChecked(106,true);
     mbar.setEnabled(106,false);
     mbar.setChecked(4, true);
     
    New format:
     // declare these as global variables:
     MenuItem mi4, mi106;
    
     // at initUI
     setMenuBar(mbar = new MenuBar(MenuBar.strings2items(new String[][]{menuArq,menuUtil,menuSobre})));
     MenuItem[][] mis = mbar.getMenuItems();
     mi4 = mis[0][4];
     mi106 = mis[1][6];
     mi106.isChecked = true;
     mi106.isEnabled = false;
     mi4.isChecked = true;
     
    Note that, after changing the isChecked and isEnabled states, there's no need to call repaint, bacause they will show up only the next time the menu bar opens.
    • Field Detail

      • gap

        public int gap
        Note: if you want to change the spacement between the menu items (maybe to make more items fit in the row), change this gap value. Default is 3 (3 pixels at left and 3 at right).
    • Constructor Detail

      • MenuBar

        public MenuBar​(MenuItem[][] items)
        Create a MenuBar with the given menu items.
    • Method Detail

      • strings2items

        public static MenuItem[][] strings2items​(java.lang.String[][] items)
        Converts a String matrix into a MenuItem matrix.
      • getMenuItems

        public MenuItem[][] getMenuItems()
        Returns the matrix of Menuitems passed in the constructor.
      • getMenuItem

        public MenuItem getMenuItem​(int index)
        Returns the MenuItem at the given index. E.g.:
         MenuItem file = mbar.getMenuItem(102);
         
      • setPopColors

        public void setPopColors​(int back,
                                 int fore,
                                 int cursor)
        Set the colors for the popup windows. You can pass -1 to any parameter to keep the current settings.
      • setCursorColor

        public void setCursorColor​(int c)
        Sets the cursor color. By default, it is based in the background color
      • setVisible

        public void setVisible​(boolean b)
        Called by the Window class to popup this MenuBar
        Overrides:
        setVisible in class Control
      • onFontChanged

        protected void onFontChanged()
        Change the font and recompute some parameters
        Overrides:
        onFontChanged in class Control
      • getSelectedIndex

        public int getSelectedIndex()
        Returns the current menu item selected.
        Returns:
        -1 if no item was selected, or a number with format xyy, where x is the index of the item selected at the MenuBar and yy is the index+1 ( = the index of the String array that defines the items) of the item selected in the MenuBarDropDown. EG: suppose that the col1/"Cut" of the example at the top of this page was clicked, then 102 is returned.
      • onPopup

        protected void onPopup()
        Setup some important variables
        Overrides:
        onPopup in class Window
      • onClickedOutside

        protected boolean onClickedOutside​(PenEvent event)
        Close the popup list with a click outside its bounds
        Overrides:
        onClickedOutside in class Window
      • onEvent

        public void onEvent​(Event event)
        Description copied from class: Control
        Called to process key, pen, control and other posted events.
        Overrides:
        onEvent in class Control
        Parameters:
        event - the event to process
        See Also:
        Event, KeyEvent, PenEvent
      • postUnpop

        protected void postUnpop()
        Description copied from class: Window
        Placeholder called after the unpop is done and after the repaint of the other window. The default implementation does nothing.
        Overrides:
        postUnpop in class Window
      • postPopup

        protected void postPopup()
        Description copied from class: Window
        Placeholder called after the popup is done and after the repaint of this window. The default implementation does nothing.
        Overrides:
        postPopup in class Window
      • switchTo

        protected void switchTo​(int index)
      • onColorsChanged

        protected void onColorsChanged​(boolean colorsChanged)
        Description copied from class: Control
        Called after a setEnabled, setForeColor and setBackColor and when a control has been added to a Container. If colorsChanged is true, it was called from setForeColor/setBackColor/Container.add; otherwise, it was called from setEnabled
        Overrides:
        onColorsChanged in class Container
      • onPaint

        public void onPaint​(Graphics g)
        Description copied from class: Container
        Draws the border (if any). If you override this method, be sure to call super.onPaint(g);, or the border will not be drawn.
        Overrides:
        onPaint in class Container
        Parameters:
        g - the graphics object for drawing
        See Also:
        Graphics
      • moveBy

        public void moveBy​(int i)
        Moves to the MenuItem array at left or right depending on the passed value (-1 or +1).
      • handleFocusChangeKeys

        protected boolean handleFocusChangeKeys​(KeyEvent ke)
        Description copied from class: Window
        Called by the main event handler to handle the focus change keys. Only called when Settings.keyboardFocusNavigation is true.
        Overrides:
        handleFocusChangeKeys in class Window
      • setAlternativeStyle

        public void setAlternativeStyle​(int back,
                                        int fore)
        Sets the style of this menubar to an alternative style (no borders, rectangular appearance). Here's a sample, taken from the UIGadgets sample:
         mbar.setAlternativeStyle(Color.BLUE,Color.WHITE);
         
        The colors are set using a combination of the given back and fore colors. You can have a more flexible color selection by using the code below, instead of calling this method. Under most situations, tho, this method is enough.
                  mbar.setBackForeColors(Color.BLUE, Color.WHITE);
                  mbar.setCursorColor(0x6464FF);
                  mbar.setBorderStyle(NO_BORDER);
                  mbar.setPopColors(0x0078FF, Color.CYAN, -1); // use the default cursor color for the popup menu (last -1 param)
         
        Since:
        TotalCross 1.0 beta 4