Package totalcross.ui

Class TabbedContainer

  • All Implemented Interfaces:
    ControlAnimation.AnimationFinished, Scrollable

    public class TabbedContainer
    extends ClippedContainer
    implements Scrollable, ControlAnimation.AnimationFinished
    TabbedContainer is a bar of text or image tabs. It is assumed that all images will have the same height, but they may have different widths.
    A scroll is automatically added when the total width of the titles is bigger than the control's width.
    The containers are created automatically and switched when the user press the corresponding tab.

    Here is an example showing a tab bar being used:

     public class MyProgram extends MainWindow
     {
        TabbedContainer tab;
    
        public void initUI()
        {
           String names[] = {"Edition","Report"};
           tab = new TabbedContainer(names);
           add(tab);
           tab.setGaps(2,2,2,2); // set it before setting the rect
           tab.setRect(LEFT,TOP,FILL,FILL);
           tab.setContainer(0,new Edition()); // replace container 1 by a class that extends Container.
           tab.getContainer(1).add(new Label("Not implemented"),CENTER,CENTER);
        }
    
        public void onEvent(Event event)
        {
           if (event.type == ControlEvent.PRESSED && event.target == tp)
           {
              int activeIndex = tp.getActiveTab();
              ... handle tab being pressed
           }
        }
     }
     
    Here's another sample that will show two TabbedContainers, one with images and another one with scrolling tabs. Note that you must create img1.png and img2.png.
     TabbedContainer tp1 = new TabbedContainer(new Image[]{new Image("img1.png"), new Image("img2.png")}, null);
     add(tp1);
     tp1.setRect(LEFT,TOP,Settings.screenWidth/2,Settings.screenHeight/2);
     tp1.activeTabBackColor = Color.getRGB(222,222,222);
    
     TabbedContainer tp2 = new TabbedContainer(new String[]{"verinha","marcelo","denise","guilherme","renato","michelle","rafael","barbara","lucas","ronaldo","nenem",});
     add(tp2);
     tp2.setRect(LEFT,AFTER+2,FILL,FILL);
     
    When the user interface is Android, the tabs do not look good if the background is the same of the parent's. In this case, we force the background to be slighly darker. There are a few fields that you can use to change the color, like activeTabBackColor, useOnTabTheContainerColor and pressedColor. Important: starting in TotalCross 1.3, with Settings.fingerTouch=true, you CANNOT call setRect in your container. Otherwise, the flick and drag will not work and your container will be positioned incorrectly.
    • Field Detail

      • arrowsColor

        public int arrowsColor
        Set the arrows color right after the constructor and after calling setCaptionsColor, which also change this property.
      • useBorder2

        public boolean useBorder2
        Set to true to enable the alternative tab border
      • autoShrinkCaptions

        public boolean autoShrinkCaptions
        Set to true to automatically shrink the captions to prevent using arrows. Works only for String-based captions.
      • showArrows

        public boolean showArrows
        Enables or not the arrows if scroll is needed.
      • tabsBackColor

        public int[] tabsBackColor
        Sets the colors used on each tab. You must create and set the array with the colors. Pass -1 to keep the original color. This array has precedence over the other ways that changes colors, except activeTabBackColor.
        Since:
        TotalCross 1.52
        See Also:
        Control.setBackColor(int), useOnTabTheContainerColor, activeTabBackColor
      • lastActiveTab

        public int lastActiveTab
        Stores the last active tab index, or -1 if none was previously selected.
        Since:
        SuperWaba 4.21
      • flickIntoDisabledTabs

        public boolean flickIntoDisabledTabs
        In finger touch devices, the user still can flick into a disabled tab. To disable this behaviour, set this flag to false; so when a disabled tab is reached, the user will not be able to flick into it, and will have to click on an enabled tab to continue flicking.
        Since:
        TotalCross 1.3
      • TABS_TOP

        public static final byte TABS_TOP
        To be used on the setType method: specifies that the tabs will be placed on the top.
        See Also:
        Constant Field Values
      • TABS_BOTTOM

        public static final byte TABS_BOTTOM
        To be used on the setType method: specifies that the tabs will be placed on the bottom.
        See Also:
        Constant Field Values
      • TABS_NONE

        public static final byte TABS_NONE
        To be used on the setType method: specifies that the tabs will be hidden and you will be responsible to change them.
        See Also:
        Constant Field Values
      • pressedColor

        public int pressedColor
        Set the color when the user clicks on the tab.
        Since:
        TotalCross 1.3.4
      • allSameWidth

        public boolean allSameWidth
        Set to true to make all tabs have the same width.
        Since:
        TotalCross 1.3.4
      • extraTabHeight

        public int extraTabHeight
        Define an extra height for the tabs. Use something line fmH/2. Required when setIcons is called.
        Since:
        TotalCross 1.3.4
        See Also:
        setIcons(Image[])
      • unselectedTextColor

        public int unselectedTextColor
        The color used for the text of unselected tabs. Defaults to the foreground color.
      • flick

        protected Flick flick
        The Flick object listens and performs flick animations on PenUp events when appropriate.
      • allowFlick

        public static boolean allowFlick
        Set to false to disable flicking between tabs. You can still switch between the tabs by clicking on them. Sample:
              TabbedContainer.allowFlick = false;
              TabbedContainer tc = new TabbedContainer(caps);
              TabbedContainer.allowFlick = true;
         
      • animationTime

        public int animationTime
        Animation time when you click in the tab. Set to 0 to disable animation.
    • Constructor Detail

      • TabbedContainer

        public TabbedContainer​(java.lang.String[] strCaptions)
        Constructs a tab bar control with Strings as captions.
      • TabbedContainer

        public TabbedContainer​(Image[] imgCaptions,
                               int transparentColor)
        Constructor to keep compilation compatibility with TC 1; transparentColor is ignored.
      • TabbedContainer

        public TabbedContainer​(Image[] imgCaptions)
        Constructs a tab bar control with images as captions, using the given color as transparent color. If you don't want to use transparent colors, just pass -1 to the color.
    • Method Detail

      • 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
      • getTabCount

        public int getTabCount()
        Returns the number of tabs.
        Since:
        TotalCross 1.15
      • setEnabled

        public void setEnabled​(int tabIndex,
                               boolean on)
        Sets the given tab index as enabled or not. When a tab is disabled, it is displayed faded, and if the user clicks on it, nothing happens. However, you still can activate it by calling setActiveTab. If there are no tabs enabled, the current tab will be made active and the controls will also be enabled. So, if you plan to disable all tabs, better disable the TabbedContainer control instead.
        Parameters:
        on - If true, the tab is enabled, if false it is disabled.
        tabIndex - The tab's index (0 to count-1)
        Since:
        TotalCross 1.01
        See Also:
        setActiveTab(int)
      • isEnabled

        public boolean isEnabled​(int tabIndex)
        Returns if the given tab index is enabled.
        Since:
        TotalCross 1.01
      • setActiveIcon

        public void setActiveIcon​(Image newActiveIcon)
        Sets the active icon.
      • setIcons

        public void setIcons​(Image[] icons)
        Set the given icons to appear at the top (or bottom, if TABS_BOTTOM) of a text TabbedContainer. The icon images must be squared. You must also set the extraTabHeight, because the icons will be resized to (extraTabHeight-fmH) in both directions.
        Since:
        TotalCross 1.3.4
      • setIcons

        public void setIcons​(Image[] icons,
                             Image activeIcon)
        Set the given icons to appear at the top (or bottom, if TABS_BOTTOM) of a text TabbedContainer. The icon images must be squared. You must also set the extraTabHeight, because the icons will be resized to (extraTabHeight-fmH) in both directions. Also, sets the active icon.
      • setType

        public void setType​(byte type)
        Sets the position of the tabs. use constants TABS_TOP or TABS_BOTTOM. Since the tabs are not changed dinamicaly, this method must be called after the constructor.
      • getType

        public int getType()
        Returns the tabs type.
      • getContainer

        public Container getContainer​(int i)
        Returns the Container for tab i
      • setContainer

        public void setContainer​(int i,
                                 Container container)
        Replaces the default created Container with the given one. This way you can avoid adding a container to a container and, as such, waste memory. Note that you must do this before the first setRect for this TabbedContainer; otherwise, you must explicitly call setRect again to update the added container bounds
      • setActiveTab

        public void setActiveTab​(int tab)
        Sets the currently active tab, animating it. A PRESSED event will be posted to the given tab if it is not the currently active tab; then, the containers will be switched.
      • setActiveTab

        public void setActiveTab​(int tab,
                                 boolean animate)
        Sets the currently active tab. A PRESSED event will be posted to the given tab if it is not the currently active tab; then, the containers will be switched. The animation is optional and can also be defined with @see
      • getActiveTab

        public int getActiveTab()
        Returns the index of the selected tab
      • getActiveContainer

        public Container getActiveContainer()
        Returns the container of the active tab.
        Since:
        TotalCross 1.2
      • getPreferredHeight

        public int getPreferredHeight()
        Returns the caption height for this TabbedContainer. Note that it is not possible to compute the correct height of each container, since they will be added AFTER this TabbedContainer has their bounds set. So, you should actually use some other way to specify the bounds, like FILL or FIT; using PREFERRED in the height of setRect will make your application abort.
        Overrides:
        getPreferredHeight in class Control
      • getPreferredWidth

        public int getPreferredWidth()
        Returns the minimum width (based on the sizes of the captions) for this TabbedContainer
        Overrides:
        getPreferredWidth in class Control
      • onBoundsChanged

        protected void onBoundsChanged​(boolean screenChanged)
        Used internally. resizes all the containers and add the arrows if scroll is needed.
        Overrides:
        onBoundsChanged in class Control
        Parameters:
        screenChanged - If the bounds were changed due to a screen change (rotation, collapse)
      • setEnabled

        public void setEnabled​(boolean b)
        Description copied from class: Container
        Sets if this container and all childrens can or not accept events
        Overrides:
        setEnabled in class Container
      • computeTabsRect

        public void computeTabsRect()
        compute the rects that represents each tab on the screen.
      • onFontChanged

        public void onFontChanged()
        Compute the rectangles of the tabs based on the selected (bolded) and unselected (plain) titles.
        Overrides:
        onFontChanged in class Control
      • 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)
        Called by the system to draw the tab bar.
        Overrides:
        onPaint in class Container
        Parameters:
        g - the graphics object for drawing
        See Also:
        Graphics
      • getTabColor

        public int getTabColor​(int tab)
        Returns the color of the given tab.
        Since:
        TotalCross 1.52
      • setCaptionColor

        public void setCaptionColor​(int capColor)
        Sets the text color of the captions in the tabs.
      • getCaptionColor

        public int getCaptionColor()
        Gets the text color of the captions. return a grayed value if this control is not enabled.
      • getClientRect

        protected void getClientRect​(Rect r)
        Returns the area excluding the tabs and borders for this TabbedContainer. In this version, you provide the created Rect to be filled with the coords.
        Overrides:
        getClientRect in class Container
      • onEvent

        public void onEvent​(Event event)
        Called by the system to pass events to the tab bar control.
        Overrides:
        onEvent in class Control
        Parameters:
        event - the event to process
        See Also:
        Event, KeyEvent, PenEvent
      • changeHighlighted

        public void changeHighlighted​(Container p,
                                      boolean forward)
        Tranfer the focus between the containers on this TabbedContainer
        Overrides:
        changeHighlighted in class Control
      • setHighlighting

        public void setHighlighting()
        Only return to highlighting when we want
        Overrides:
        setHighlighting 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 Control
      • getFocusableControls

        public void getFocusableControls​(Vector v)
        Description copied from class: Container
        Get a list of child controls of this container which are focus candidates
        Overrides:
        getFocusableControls in class Container
        Parameters:
        v - A vector into which to add the focus candidates.
      • handleGeographicalFocusChangeKeys

        public Control handleGeographicalFocusChangeKeys​(KeyEvent ke)
        Description copied from class: Control
        Used by the main event loop to give the currently focused control an opportunity to act directly on the KeyEvent.
        Overrides:
        handleGeographicalFocusChangeKeys in class Control
        Parameters:
        ke - The KeyEvent to be processed
        Returns:
        The control that should get focus as a result of this KeyEvent. Null if this control did not handle the KeyEvent.
        See Also:
        Settings.geographicalFocus
      • isAtTop

        @Deprecated
        public boolean isAtTop()
        Deprecated.
        Use getType
        Returns true of the type is set to TABS_TOP.
      • flickStarted

        public boolean flickStarted()
        Description copied from interface: Scrollable
        Called when the flick animation is started.
        Specified by:
        flickStarted in interface Scrollable
      • flickEnded

        public void flickEnded​(boolean atPenDown)
        Description copied from interface: Scrollable
        Called when the flick animation is ended.
        Specified by:
        flickEnded in interface Scrollable
        Parameters:
        atPenDown - Flag indicating if the animation ended due to a pendown event.
      • canScrollContent

        public boolean canScrollContent​(int direction,
                                        java.lang.Object target)
        Description copied from interface: Scrollable
        Checks if there is room to flick this container in the given direction.
        Specified by:
        canScrollContent in interface Scrollable
        Parameters:
        direction - The direction we want to flick given by one of the following constants:
        target - The target of the series of PEN events that triggered this flick attempt.
        Returns:
        true if the container can flick in the indicated direction.
      • scrollContent

        public boolean scrollContent​(int xDelta,
                                     int yDelta,
                                     boolean fromFlick)
        Description copied from interface: Scrollable
        Performs a relative move.
        Specified by:
        scrollContent in interface Scrollable
        Parameters:
        xDelta - The relative amount of pixels to move in the X axis.
        yDelta - The relative amount of pixels to move in the Y axis.
      • getScrollPosition

        public int getScrollPosition​(int direction)
        Description copied from interface: Scrollable
        Returns the current position given the direction. Used on page scrolls.
        Specified by:
        getScrollPosition in interface Scrollable
      • getFlick

        public Flick getFlick()
        Description copied from interface: Scrollable
        Returns the current flick object.
        Specified by:
        getFlick in interface Scrollable
      • wasScrolled

        public boolean wasScrolled()
        Description copied from interface: Scrollable
        Returns true if the control was scrolled since last pen down
        Specified by:
        wasScrolled in interface Scrollable
      • setCaptions

        public void setCaptions​(java.lang.String[] caps)
        Changes the tab captions. The new array must have the same length or an exception is thrown.