Package totalcross.ui

Class ScrollContainer

  • All Implemented Interfaces:
    Scrollable
    Direct Known Subclasses:
    ButtonMenu, Document, DynamicScrollContainer, ListContainer

    public class ScrollContainer
    extends Container
    implements Scrollable
    ScrollContainer is a container with a horizontal only, vertical only, both or no ScrollBars, depending on the control positions. The default unit scroll is an Edit's height (for the vertical scrollbar), and the width of '@' (for the horizontal scrollbar).

    Caution: you must not use RIGHT, BOTTOM, CENTER and FILL when setting the control bounds, unless you disable the corresponding ScrollBar! The only exception to this is to use FILL on the control's height, which is allowed.

    Here is an example showing how it can be used:

     public class MyProgram extends MainWindow
     {
     ScrollContainer sc;
    
     public void initUI()
     {
           ScrollContainer sc;
           add(sc = new ScrollContainer());
           sc.setBorderStyle(BORDER_SIMPLE);
           sc.setRect(LEFT+10,TOP+10,FILL-20,FILL-20);
           int xx = new Label("Name99").getPreferredWidth()+2; // edit's alignment
           for (int i =0; i < 100; i++)
           {
              sc.add(new Label("Name"+i),LEFT,AFTER);
              sc.add(new Edit("@@@@@@@@@@@@@@@@@@@@"),xx,SAME);
              if (i % 3 == 0) sc.add(new Button("Go"), AFTER+2,SAME,PREFERRED,SAME);
           }
     }
    }
     
    • Field Detail

      • sbH

        public ScrollBar sbH
        Returns the scrollbar for this ScrollContainer. With it, you can directly set its parameters, like blockIncrement, unitIncrement and liveScrolling. But be careful, don't mess with the minimum, maximum and visibleItems. If using Android or Material user interface style, these are replaced by the ScrollPosition. If you set sbV.transparentBackground=true (or for sbH), you will expand the client area to over the ScrollPosition's area.
      • sbV

        public ScrollBar sbV
        Returns the scrollbar for this ScrollContainer. With it, you can directly set its parameters, like blockIncrement, unitIncrement and liveScrolling. But be careful, don't mess with the minimum, maximum and visibleItems. If using Android or Material user interface style, these are replaced by the ScrollPosition. If you set sbV.transparentBackground=true (or for sbH), you will expand the client area to over the ScrollPosition's area.
      • flick

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

        protected int lastV
      • lastH

        protected int lastH
      • shrink2size

        public boolean shrink2size
        Set to true, to make the surrounding container shrink to its size.
      • autoScroll

        public boolean autoScroll
        Automatically scrolls the container when an item is clicked.
        See Also:
        hsIgnoreAutoScroll
      • hsIgnoreAutoScroll

        public static java.util.HashSet<java.lang.Class<?>> hsIgnoreAutoScroll
        Defines a list of classes that will make autoScroll be ignored. Use it like:
         ScrollContainer.hsIgnoreAutoScroll.add(totalcross.ui.SpinList.class);
         ...
         
        This is useful if such class usually requires more than one press to have a value defined.
        See Also:
        autoScroll
    • Constructor Detail

      • ScrollContainer

        public ScrollContainer()
        Standard constructor for a new ScrollContainer, with both scrollbars enabled.
      • ScrollContainer

        public ScrollContainer​(boolean allowScrollBars)
        Constructor used to specify when both scrollbars are enabled or not.
      • ScrollContainer

        public ScrollContainer​(boolean allowHScrollBar,
                               boolean allowVScrollBar)
        Constructor used to specify when each scrollbar is enabled or not. By disabling the horizontal scrollbar, you can use RIGHT and CENTER on the x parameter of a control that is added. By disabling the vertical scrollbar, you can use BOTTOM and CENTER on the y parameter of a control that is added.
        Since:
        TotalCross 1.27
    • Method Detail

      • getRealClientRect

        public Rect getRealClientRect()
        Returns the client rect from the ScrollContainer control
      • setScrollBars

        protected void setScrollBars​(boolean allowHScrollBar,
                                     boolean allowVScrollBar)
      • getBagInitialHeight

        protected int getBagInitialHeight()
        Overwrite this method to define a custom height for the scrolling area
      • getBagInitialWidth

        protected int getBagInitialWidth()
        Overwrite this method to define a custom width for the scrolling area
      • 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 dx,
                                     int dy,
                                     boolean fromFlick)
        Description copied from interface: Scrollable
        Performs a relative move.
        Specified by:
        scrollContent in interface Scrollable
        Parameters:
        dx - The relative amount of pixels to move in the X axis.
        dy - 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
      • add

        public void add​(Control control)
        Adds a child control to the bag container.
        Overrides:
        add in class Container
      • remove

        public void remove​(Control control)
        Removes a child control from the bag container.
        Overrides:
        remove in class Container
      • onBoundsChanged

        protected void onBoundsChanged​(boolean screenChanged)
        Description copied from class: Control
        Called after a setRect.
        Overrides:
        onBoundsChanged in class Control
        Parameters:
        screenChanged - If the bounds were changed due to a screen change (rotation, collapse)
      • 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
      • bagSetRect

        protected void bagSetRect​(int x,
                                  int y,
                                  int w,
                                  int h,
                                  boolean screenChanged)
      • resize

        public void resize()
        This method resizes the control to the needed bounds, based on added childs. Must be called if you're controlling reposition by your own, after you repositioned the controls inside of it.
        Overrides:
        resize in class Container
        See Also:
        Container.resizeWidth(), Container.resizeHeight()
      • resize

        public void resize​(int maxX,
                           int maxY)
        This method resizes the control to the needed bounds, based on the given maximum width and heights.
      • canShowScrollBars

        protected boolean canShowScrollBars​(boolean vertical)
      • getScrollDistance

        public int getScrollDistance()
        Override this method to return the correct scroll distance. Defaults to the container's width.
      • reposition

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

        public int getPreferredWidth()
        Returns the preferred width AFTER the resize method was called. If the ScrollBars are disabled, returns the maximum size of the container to hold all controls.
        Overrides:
        getPreferredWidth in class Control
      • getPreferredHeight

        public int getPreferredHeight()
        Returns the preferred height AFTER the resize method was called. If the ScrollBars are disabled, returns the maximum size of the container to hold all controls.
        Overrides:
        getPreferredHeight in class Control
      • 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
      • 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
      • scrollToPage

        public void scrollToPage​(int p)
        Scrolls to the given page, which is the flick's scrollDistance (if set), or the control's height.
        Since:
        TotalCross 1.53
      • scrollPage

        public void scrollPage​(boolean left)
        Scrolls a page to left or right. Works only if it has a flick and a page position.
      • scrollToControl

        public void scrollToControl​(Control c)
        Scrolls to the given control.
      • scrollToOrigin

        public void scrollToOrigin()
        Scroll the container to origin position 0,0
      • setVValue

        protected void setVValue​(int val)
        Sets the vertical's ScrollBar value to the given one.
      • setHValue

        protected void setHValue​(int val)
        Sets the horizontal's ScrollBar value to the given one.
      • 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
      • removeAll

        public void removeAll()
        Removes all controls from the ScrollContainer.
        Overrides:
        removeAll in class Container
      • getBagChildren

        public Control[] getBagChildren()
        Returns the children of the bag. If you call ScrollContainer.getChildren, it will not return the controls added to the ScrollContainer, since they are actually added to the bag.
        Since:
        TotalCross 1.5
      • onFontChanged

        public void onFontChanged()
        Description copied from class: Control
        Called after a setFont
        Overrides:
        onFontChanged in class Control
      • moveFocusToNextControl

        public Control moveFocusToNextControl​(Control control,
                                              boolean forward)
        Description copied from class: Container
        Moves the focus to the next control, which can be an Edit, a MultiEdit, or another control type. It does not show the keyboard.
        Overrides:
        moveFocusToNextControl in class Container
        Returns:
        The selected control or null if none was found.