Package totalcross.ui

Class ButtonMenu

  • All Implemented Interfaces:
    EventHandler, PressListener, UpdateListener, Scrollable

    public class ButtonMenu
    extends ScrollContainer
    implements PressListener
    This class adds a multi-button menu that can be scrolled horizontally (single-row) or vertically (multiple-rows), using a scrollbar or flicking. The buttons can have almost all properties present in the Button class, like:
    • textPosition
    • borderType
    • cornerRadius3DG
    • borderWidth3DG
    • borderColor3DG
    • topColor3DG
    • bottomColor3DG
    There are also other properties that can be set, like:
    • textGap
    • buttonVertGap
    • buttonHorizGap
    • imageSize
    • borderGap
    The sizes above are not in pixels, but in percentage of the font's height. So, a value of 25 means 25% of the font's height, or 1/4; 150 means 150% of the font's height, or 1.5x; and so on. This enabled the gaps be constant in physical inches no matter the screen DPI or resolution. See the AndroidUI program for a sample of how to use this class.
    Since:
    TotalCross 1.3
    See Also:
    pagePositionDisposition
    • Field Detail

      • pagepos

        protected PagePosition pagepos
        The control that keeps track of the current position.
      • textGap

        public int textGap
        The gap between the image and the text, in percentage of the font's height. Defaults to 25 (%).
      • buttonVertGap

        public int buttonVertGap
        The gap between two vertical buttons, in percentage of the font's height. Also used as gap between the button and the ButtonMenu's border. Defaults to 100 (%).
      • buttonHorizGap

        public int buttonHorizGap
        The gap between two horizontal buttons, in percentage of the font's height. Also used as gap between the button and the ButtonMenu's border. Defaults to 100 (%).
      • imageSize

        public int imageSize
        The size of the image, in percentage of the font's height. Defaults to 200 (%). Set to -1 to keep the original size.
      • borderGap

        public int borderGap
        The gap between the text or image and the button borders, in percentage of the font's height. Defaults to 10 (%).
      • PAGEPOSITION_AT_BOTTOM

        public static final int PAGEPOSITION_AT_BOTTOM
        Used in the pagePositionDisposition. Place it at bottom.
        See Also:
        Constant Field Values
      • PAGEPOSITION_AT_TOP

        public static final int PAGEPOSITION_AT_TOP
        Used in the pagePositionDisposition. Place it at top.
        See Also:
        Constant Field Values
      • NO_PAGEPOSITION

        public static final int NO_PAGEPOSITION
        Used in the pagePositionDisposition. Don't use a PagePosition, use the ScrollPosition instead.
        See Also:
        Constant Field Values
      • textPosition

        public int textPosition
        Where to place the text (supports only LEFT, TOP, RIGHT, BOTTOM, CENTER - no adjustments!). Also supports RIGHT_OF (relativeToText is computed automatically). Defaults to CENTER.
      • SINGLE_COLUMN

        public static final int SINGLE_COLUMN
        Used in the disposition member of the constructor. The menu will have a single column and multiple rows and will scroll vertically.
        See Also:
        Constant Field Values
      • SINGLE_ROW

        public static final int SINGLE_ROW
        Used in the disposition member of the constructor. The menu will have a single row and multiple columns and will scroll horizontally.
        See Also:
        Constant Field Values
      • MULTIPLE_HORIZONTAL

        public static final int MULTIPLE_HORIZONTAL
        Used in the disposition member of the constructor. The menu will have multiple columns and rows and will scroll horizontally.
        See Also:
        Constant Field Values
      • MULTIPLE_VERTICAL

        public static final int MULTIPLE_VERTICAL
        Used in the disposition member of the constructor. The menu will have multiple columns and rows and will scroll vertically.
        See Also:
        Constant Field Values
    • Method Detail

      • replaceWith

        public void replaceWith​(Image[] images,
                                java.lang.String[] names)
        Changes all the buttons to the given parameters. If you don't want to set the images or the names, pass null in the proper place. Calls onFontChanged and initUI to reset the buttons.
        Since:
        TotalCross 1.66
      • getButton

        public Button getButton​(int idx)
        Returns the button at the given index. You may customize it.
        Since:
        TotalCross 2.0
      • onFontChanged

        public void onFontChanged()
        Creates and resizes all Button and images. For better performance, call setFont for this control BEFORE calling add or setRect (this is a general rule for all other controls as well).
        Overrides:
        onFontChanged in class ScrollContainer
      • createButton

        protected Button createButton​(java.lang.String name,
                                      Image img,
                                      int textPosition,
                                      int tg)
      • initUI

        public void initUI()
        Description copied from class: Container
        Called to initialize the User Interface of this container. This differs from the onAddAgain method by that this method is called only once, at the first time the control is added to the parent. When the container is being setup, the initUI method is called; then, the onAddAgain is called every time the container is added again.
        Overrides:
        initUI in class Container
      • reposition

        public void reposition()
        Description copied from class: Control
        Reposition this control, calling again setRect with the original parameters.
        Overrides:
        reposition in class ScrollContainer
      • getPreferredWidth

        public int getPreferredWidth()
        Returns the preferred width as if all images were in a single row.
        Overrides:
        getPreferredWidth in class ScrollContainer
      • getPreferredWidth

        public int getPreferredWidth​(int cols)
        Returns the preferred width for the given number of columns.
      • getPreferredHeight

        public int getPreferredHeight()
        Returns the preferred height as if all images were in a single row.
        Overrides:
        getPreferredHeight in class ScrollContainer
      • getPreferredHeight

        public int getPreferredHeight​(int rows)
        Returns the preferred height for the given number of rows. For example:
         add(ib2,LEFT+10,CENTER,FILL-10,ib2.getPreferredHeight(4));
         
      • getSelectedIndex

        public int getSelectedIndex()